Dodomeki
Sign in

100. Subarrays with k Distinct Values

Hard

Given an array of integers and k, return the number of contiguous subarrays that contain exactly k distinct values. If k is zero or greater than the number of distinct values in the array, the answer is zero.

Example 1
Input: nums = [1,2,1,2,3], k = 2
Output: 7
Example 2
Input: nums = [1,2,1,3,4], k = 3
Output: 3
Constraints
  • 0 <= nums.length <= 10000
  • 0 <= k <= 10000
ArraysHash mapSliding window
Caso 1
nums =
[1,2,1,2,3]
k =
2
Expected =
7
Caso 2
nums =
[1,2,1,3,4]
k =
3
Expected =
3

Run checks these 2 cases. Submit runs all 6, including hidden ones.