This question evaluates proficiency in array manipulation, frequency counting, and efficient tracking of distinct values within contiguous subarrays, measuring algorithmic problem-solving and data-structure usage.
Given an integer array nums and an integer k, find the length of the shortest contiguous subarray that contains at least k distinct integers.
Return the minimum length of such a subarray. If no subarray contains at least k distinct integers, return -1.
nums
(length
n
), integer
k
-1
)
1 <= n <= 2*10^5
1 <= k <= n
nums[i]
fits in 32-bit signed integer
nums = [1,2,1,3,4], k = 3
→ answer
3
(e.g.,
[2,1,3]
)
nums = [1,1,1], k = 2
→ answer
-1