The Question
CodingCount Subarrays with at Most K Distinct Integers
Given an integer array
nums and an integer k, return the number of contiguous subarrays that contain at most k distinct integers.
Example:
Input: nums = [1,2,1,2,3], k = 2
Output: 12
Explanation: Subarrays formed with at most 2 distinct integers: [1], [2], [1], [2], [3], [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
Constraints:
- 1 <= nums.length <= 10^5
- 0 <= nums[i] <= 10^9
- 0 <= k <= nums.lengthJava
Sliding Window
Two Pointers
Hash Map