diff --git a/992. Subarrays with K Different Integers b/992. Subarrays with K Different Integers new file mode 100644 index 0000000..c410e11 --- /dev/null +++ b/992. Subarrays with K Different Integers @@ -0,0 +1,37 @@ +class Solution { +public: + int findAtMostK(int n, int k, vector&nums){ + int ans = 0; + int left = 0, right = 0; + unordered_mapmp; + while(right k && left<=right){ + mp[nums[left]]--; + if(mp[nums[left]]==0){ + mp.erase(nums[left]); + } + left++; + } + ans += right-left+1; + right++; + } + return ans; + } + int subarraysWithKDistinct(vector& nums, int k) { + int n = nums.size(); + return findAtMostK(n, k, nums) - findAtMostK(n, k-1, nums); + } +}; +auto init = [](){ + ios::sync_with_stdio(false); + cin.tie(nullptr); + cout.tie(nullptr); + return 'c'; +}();