Skip to content

Sliding Window Maximum

HardSliding Window
Decode it first. Clues, constraints, pattern and plan before any code.

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.

nums = [1, 3, -1, -3, 5, 3, 6, 7], k = 3
→ [3, 3, 5, 5, 6, 7]
 
window [1  3  -1] -3  5  3  6  7   max 3
 1 [3  -1  -3] 5  3  6  7          max 3
 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