Skip to content
Problem 239 · LeetCode 239(opens in a new tab)

Sliding Window Maximum

HardHeaps

You are given a list of whole numbers, nums, and a window size k. Picture a frame that covers k neighbouring numbers. It starts over the first k, then slides right one place at a time until it covers the last k.

For every position of the frame, record the largest number inside it. Return those maximums in order, from the leftmost window to the rightmost.

Example 1

Input:  nums = [2, 7, 4, 1, 6, 3], k = 3
Output: [7, 7, 6, 6]
Why:    the windows are [2,7,4], [7,4,1], [4,1,6], [1,6,3]
2 more examples and the constraints

Example 2

Input:  nums = [5], k = 1
Output: [5]
Why:    one window holding one number

Example 3

Input:  nums = [9, 8, 7, 6], k = 2
Output: [9, 8, 7]
Why:    each window’s max is its left end

Constraints

  • 1 ≤ length of nums ≤ 100,000
  • Every number is between −10,000 and 10,000
  • 1 ≤ k ≤ length of nums

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. Scan every windowO(n·k)
  2. Max-heap with lazy removalO(n log n)
  3. Monotonic dequeO(n)

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeRelative work for 100,000 numbers, k = 50,000Verdict
Scan every windowO(n·k)
Mention it, then move on
Max-heap with lazy removalO(n log n)
Passes; easy to reach for
Monotonic dequeO(n)
Best; each index in and out once

n = how many numbers, k = the window size.

Edge cases to test

  • k = 1. Every window is one number, so the answer is nums itself.
  • k = n. There is one window, and the answer is a list holding the overall max.
  • Repeated values. [4, 4, 4, 4] with k = 2 gives [4, 4, 4]. Popping equal values off the back is fine: the newer copy stays longer.
  • The max slides out. In [1, 3, 1, 2, 0, 5] with k = 3, 3 leaves and 2 must take over. That is why smaller values behind the max are kept.
  • Falling values. [9, 8, 7, 6]: nothing is ever popped from the back, so the front pop does all the work.

Common mistakes

  • Storing values instead of indices, so there is no way to tell when the front has left the window.
  • Appending to the result before the first window is full (before i = k − 1).
  • Using if instead of while when popping smaller values off the back.

How to talk through it

  1. 1Clarify. Is k always between 1 and n? Can numbers repeat or be negative? Should the result have n − k + 1 values?
  2. 2Give the brute force. Scan each window for its max: O(n·k), too slow when both are large.
  3. 3Offer the heap. A max-heap of (value, index) with stale tops popped gets O(n log n). Then ask what can be thrown away for good.
  4. 4Code the deque. A smaller number left of a bigger one is never a max again, so pop it. Say why each index moves in and out once.
  5. 5Test out loud. Run k = 1, k = n and a case where the max slides out before saying you are done.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?