Sliding window with a frequency map, not too bad once you see it.
Use a sliding window (two-pointer) technique to maintain a window with at least k distinct integers, expanding the right pointer to include more distinct values and shrinking the left pointer to find the shortest valid window. Track the frequency of each integer in the window and the number of distinct integers, updating the minimum length whenever the window is valid.
Pro tip: Clarify edge cases upfront, such as when k is greater than the total distinct integers in the array or when the array is empty, and mention that the sliding window approach achieves O(n) time and O(n) space, which is optimal.
Restate the problem to ensure clarity: find the shortest contiguous subarray with at least k distinct integers. Discuss edge cases like empty array, k=0, k > total distinct integers, and negative numbers.
Explain that a brute-force solution would be O(n^2) or worse, so a sliding window (two-pointer) technique is optimal. Describe how the window expands and contracts to maintain at least k distinct integers.
Initialize left=0, a frequency map, distinct count, and min length. Iterate right from 0 to n-1, add element to map, update distinct count. While distinct >= k, update min length, then remove left element, update map and distinct count, and increment left.
State time complexity O(n) and space O(n) due to the frequency map. Walk through a small example to verify correctness, and mention potential optimizations or alternative approaches if needed.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.