You're given an integer array nums and an integer k. Your task is to count the number of "good" subarrays in the array.
A subarray is considered good if it contains exactly k different integers. For example, the subarray [1,2,3,1,2] has exactly 3 different integers: 1, 2, and 3.
Remember: A subarray is a contiguous part of an array, meaning all elements must be adjacent to each other in the original array.
Goal: Return the total count of all good subarrays.
Example: If nums = [1,2,1,2,3] and k = 2, then subarrays like [1,2], [2,1], [1,2,1], [2,1,2] are all good because they contain exactly 2 different integers.
Input & Output
Visualization
Time & Space Complexity
Each element is visited at most twice (once by right pointer, once by left pointer) in each of the two helper function calls
Hash map stores at most k distinct elements and their frequencies
Constraints
- 1 โค nums.length โค 2 ร 104
- 1 โค nums[i], k โค nums.length
- All integers in nums are positive