(C++) 992. Subarrays with K Different Integers
\n\nclass Solution {\npublic:\n    int subarraysWithKDistinct(vector<int>& nums, int k) {\n        int ans = 0, ii = -1; \n        unordered_map<int, int> freq;
\n```\nclass Solution {\npublic:\n    int subarraysWithKDistinct(vector<int>& nums, int k) {\n        int ans = 0, ii = -1; \n        unordered_map<int, int> freq; \n        queue<int> q; \n        \n        for (int i = 0; i < nums.size(); ++i) {\n            freq[nums[i]]++; \n            q.push(i); \n            if (freq.size() > k) {\n                ii = q.front(); q.pop(); \n                freq.erase(nums[ii]); \n            }\n            \n            while (freq[nums[q.front()]] > 1) {\n                int k = q.front(); q.pop(); \n                freq[nums[k]]--; \n            }\n            \n            if (freq.size() == k) ans += q.front() - ii; \n        }\n        return ans; \n    }\n};\n```
['C']