Sliding window maximum
Given an array nums and a window width k, return the maximum of every window of that width as it slides from one end of the array to the other. A window of width k over n values gives n − k + 1 answers, in the order the windows appear.
Examples
Example 1
- Input
- nums = [1, 3, -1, -3, 5, 3, 6, 7]k = 3
- Output
- Six windows, each reporting its largest: [1, 3, −1] → 3, then [3, −1, −3] → 3, and on to [3, 6, 7] → 7.
[3,3,5,5,6,7]
Example 2
- Input
- nums = [9, 1, 1]k = 2
- Output
- Two windows: [9, 1] gives 9 and [1, 1] gives 1.
[9,1]
The Code
function maxSlidingWindow(nums, k) {
const deque = [];
const out = [];
for (let i = 0; i < nums.length; i++) {
while (deque.length > 0 && deque[0] <= i - k) {
deque.shift();
}
while (deque.length > 0 && nums[deque[deque.length - 1]] <= nums[i]) {
deque.pop();
}
deque.push(i);
if (i >= k - 1) out.push(nums[deque[0]]);
}
return out;
}
maxSlidingWindow([1, 3, -1, -3, 5, 3, 6, 7], 3);Explanation
Ask which values in a window could ever be its maximum. A value with a bigger one after it is finished: that bigger value is in the window too, and it will still be there after the smaller one has dropped out the far end. So the only values worth remembering are the ones with nothing bigger behind them, which leaves a line falling from the largest down to the newest arrival. The largest is the window’s answer; when the window moves past it, the next one in line takes over, and each new value knocks out everything smaller waiting behind it.
- 1
dequeis empty, so position 0 joins it.ihas not reachedk − 1yet, so no window is complete and nothing is reported.i1031-12-3354356677deque=[]out=[]
deque → [0]
- 2
nums[1]is 3, and the back ofdequepoints at a 1. It is smaller and older, so it is popped before 1 joins — it can never be a maximum again.10i31-12-3354356677deque=[0]out=[]
pop 0 · deque → [1]
- 3
nums[2]is −1, smaller than the 3 in front of it, so it just joins the back.ihas reachedk − 1, so the first window is complete and the front ofdeque—nums[1], the 3 — is pushed toout.1031i-12-3354356677deque=[1]out=[]
deque → [1, 2] · out → [3]
- 4
At
i= 4 the front, position 1, has fallen out of the window and is shifted off. Thennums[4]is 5, which pops the −3 and the −1 behind it, and the front is now position 4 itself.1031-12-33i54356677deque=[1, 2, 3]out=[3, 3]
shift 1 · pop −3, −1 · out → [3, 3, 5]
- 5
The same two rules run to the end — shift the front once it leaves the window, pop anything smaller than what arrives — and
outfinishes with one answer per window.1031-12-33543566i77deque=[6]out=[3, 3, 5, 5, 6]
return [3, 3, 5, 5, 6, 7]
More like this
All arrays & loops examples (10) →- FizzBuzz The classic warm-up — Fizz, Buzz, or the number.
- Array maximum Track the largest seen so far in a single pass.
- Two sum Remember every number you’ve seen; check for its complement.
- Product except self Everything to the left of a position, times everything to its right.
- Majority element Pair off disagreeing votes; the majority always survives.
- Merge intervals Sort by start, then either extend the last interval or begin a new one.