Sliding Window Maximum
Hard
Topics
Given an array nums and a window size k, the window slides from left to right one position at a time. Return an array of the maximum value in each window.
Example 1
nums
answer
A window of 3 slides one position at a time, and each position contributes the largest value inside it. So the output has 6 entries, one per window, not one per element — the first window is braced above.
Top row is the input with the first window braced; bottom row is one maximum per window.
Input: nums = [1,3,-1,-3,5,3,6,7], k = 3 Output: [3,3,5,5,6,7]
Example 2
nums
answer
Input: nums = [1], k = 1 Output: [1]
Constraints
- 1 <= nums.length <= 10^5
- 1 <= k <= nums.length
Editorial — intuition, approach, complexity and pitfalls
Intuition
A value can never be a future maximum if a newer, larger value sits to its right. Keep a deque of indices whose values are strictly decreasing — the front is always the current window maximum.
Approach
- Maintain a deque of indices, values decreasing front to back.
- Before pushing i, pop smaller values off the back; pop the front if it fell out of the window.
- Once the first window is formed (i >= k-1), record nums[front].
Dry Run
nums = [1, 3, -1, -3, 5, 3, 6, 7], k = 3
dq holds INDICES, their values decreasing left to right
i=0 x=1 push dq=[0]
i=1 x=3 pop 0 (1 < 3), push dq=[1]
i=2 x=-1 push dq=[1, 2] window full res=[3]
i=3 x=-3 push dq=[1, 2, 3] res=[3, 3]
i=4 x=5 pop 3, 2, 1 (all < 5) dq=[4] res=[3, 3, 5]
i=5 x=3 push dq=[4, 5] res=[3, 3, 5, 5]
i=6 x=6 pop 5, 4 (both < 6) dq=[6] res=[3, 3, 5, 5, 6]
i=7 x=7 pop 6 (6 < 7) dq=[7] res=[3, 3, 5, 5, 6, 7]
answer 3 3 5 5 6 7
Note i=4: three indices leave in one step. Each index is pushed once and
popped once across the whole run, which is why this is O(n) and not O(nk)
despite the inner while loop.Complexity
- Time — O(n): each index is pushed and popped from the deque at most once.
- Space — O(k): the deque holds at most one window of indices.
Common Pitfalls
- Store indices (not values) so you can tell when the front has slid out of the window.
Full Solution — all languages
class Solution:
def maxSlidingWindow(self, nums, k):
from collections import deque
dq=deque(); res=[]
for i,x in enumerate(nums):
while dq and nums[dq[-1]]<x:
dq.pop()
dq.append(i)
if dq[0]<=i-k:
dq.popleft()
if i>=k-1:
res.append(nums[dq[0]])
return resimport java.util.*;
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
Deque<Integer> dq=new ArrayDeque<>();
List<Integer> res=new ArrayList<>();
for(int i=0;i<nums.length;i++) {
while(!dq.isEmpty()&&nums[dq.peekLast()]<nums[i])dq.pollLast();
dq.addLast(i);
if(dq.peekFirst()<=i-k)dq.pollFirst();
if(i>=k-1)res.add(nums[dq.peekFirst()]);
}
int[] r=new int[res.size()];
for(int i=0;i<r.length;i++)r[i]=res.get(i);
return r;
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: vector<int> maxSlidingWindow(vector<int>& nums, int k) {
deque<int> dq;
vector<int> res;
for(int i=0;i<(int)nums.size();i++) {
while(!dq.empty()&&nums[dq.back()]<nums[i])dq.pop_back();
dq.push_back(i);
if(dq.front()<=i-k)dq.pop_front();
if(i>=k-1)res.push_back(nums[dq.front()]);
}
return res;
}
};/**
* @param {number[]} nums
* @param {number} k
* @return {number[]}
*/ var maxSlidingWindow = function(nums, k) {
const dq=[],res=[];
for(let i=0;i<nums.length;i++) {
while(dq.length&&nums[dq[dq.length-1]]<nums[i])dq.pop();
dq.push(i);
if(dq[0]<=i-k)dq.shift();
if(i>=k-1)res.push(nums[dq[0]]);
}
return res;
};