1
5
4
2
9
9
9
[0][1][2][3][4][5][6]
Brute force · re-check every window
▸1given arr, K2best ← 03for i ← 0 to n − K:4 win = arr[i .. i+K−1]5 if win has a duplicate: skip6 best = max(best, sum(win))7return best
state
- n7
- K3
Your free access ends in 7 days — and you haven’t tried it yet. Watch one algorithm run, start to finish. It takes about two minutes.
Try one problemGiven an integer array and a length k, find the maximum sum among all contiguous subarrays of length k whose elements are all distinct.
▸1given arr, K2best ← 03for i ← 0 to n − K:4 win = arr[i .. i+K−1]5 if win has a duplicate: skip6 best = max(best, sum(win))7return best
line 1Among all subarrays of length K = 3, find the largest sum — but ONLY windows whose elements are all DISTINCT count.