Dictionary Method -With Comments- Beats 79.59% In Runtime
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n    def subarraysWithKDistinct(self, nums: List[int], k: int) -> int:\n\n        count1 = 0\n        count2 = 0\n\n        left = 0\n        right = 0\n\n        freq1 = defaultdict(int)\n        freq2 = defaultdict(int)\n\n        res = 0\n\n        for i in range(len(nums)):\n            #count frequencies which are greater than k\n            if freq1[nums[i]] == 0:\n                count1 +=1\n            freq1[nums[i]] +=1\n            #Count frequencies which are exactly k\n            if freq2[nums[i]] == 0:\n                count2 +=1\n            freq2[nums[i]] +=1\n            \n            #Checking for count greater than k\n            while count1 > k:\n                freq1[nums[right]] -=1\n                if freq1[nums[right]] == 0:\n                    count1-=1\n                right +=1\n            \n            #Checking Counts for exact k\n            while count2 > k-1:\n                freq2[nums[left]] -=1\n                if freq2[nums[left]] == 0:\n                    count2 -=1\n                left +=1\n            \n            res += left - right\n        \n        return res\n```
['Python3']