1
3
-1
-3
5
3
6
7
[0][1][2][3][4][5][6][7]
window maxima
Brute force · re-scan every window
▸1given nums, k2for i ← 0 to n − k:3 scan nums[i..i+k−1] for its max4 append max to answer5return answer
state
- n8
- 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 array nums and a window size k, return the maximum of every contiguous window of size k as the window slides from left to right.
▸1given nums, k2for i ← 0 to n − k:3 scan nums[i..i+k−1] for its max4 append max to answer5return answer
line 1For every window of size k, report its maximum. The obvious approach: slide a window across and, for each position, scan all k elements to find the biggest.