Skip to main content

Practice ยท 2 of 3

Longest Window with K Distinct

Implement static int longestWindow(int[] xs, int k) โ€” the length of the longest contiguous run containing at most k distinct values. k <= 0 โ†’ 0. Use the sliding-window pattern with a frequency map. [1,2,1,2,3], k=2 โ†’ 4 (the run [1,2,1,2]). [1,1,1], k=1 โ†’ 3. Empty โ†’ 0.

Difficulty: intermediate

Back to lesson: Practice: Algorithms Lab