Sliding Window Maximum
Hard · Monotonic Stack · Array · Queue · Sliding Window · Monotonic Deque
Problem
A window of width k slides across nums one position at a time. Return the largest value inside the window at each position.
Examples
Input: nums = [1,3,-1,-3,5,3,6,7], k = 3 Output: [3,3,5,5,6,7]
One maximum per window position.
Input: nums = [1], k = 1 Output: [1]
Constraints
- 1 <= len(nums) <= 10^5
- 1 <= k <= len(nums)
- -10^4 <= nums[i] <= 10^4
Approaches
- Recompute the maximum each window — time O(n · k), space O(1)
Related problems
Start free on Cariara · Coding problems · System design studio