Skip to content
Decoder

Sliding Window Maximum

Hard

Problem

A window of size k slides across nums from left to right, one step at a time. Return a list with the maximum of each window, in order.

Example 1

nums = [1, 3, -1, -3, 5, 3, 6, 7], k = 3

Example 2

→ [3, 3, 5, 5, 6, 7]

Example 3

window [1  3  -1] -3  5  3  6  7   max 3

Example 4

 1 [3  -1  -3] 5  3  6  7          max 3

Example 5

 1  3 [-1  -3  5] 3  6  7          max 5   ...

Constraints

  • 1 <= nums.length <= 10^5
  • -10^4 <= nums[i] <= 10^4
  • 1 <= k <= nums.length

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.