Asked at
Sliding Window Maximum
HardVerifiedQueueSliding WindowMonotonic Stack~32 min
Given an array nums and window size k, return the maximum of every contiguous length-k window, left to right.
The input arrives as a single object { nums, k }. Aim for O(n) with a monotonic deque of indices.
Examples
in{ nums: [1, 3, -1, -3, 5, 3, 6, 7], k: 3 }
out[3, 3, 5, 5, 6, 7]
Each value is the max of the current length-3 window.
in{ nums: [1], k: 1 }
out[1]
A single-element window returns that element.
Constraints
- 1 ≤ nums.length ≤ 10⁵
- -10⁴ ≤ nums[i] ≤ 10⁴
- 1 ≤ k ≤ nums.length
Get help
🔑
Sign in to solve
Sign in to write, run, and submit your solution — and to pick up where your iOS flow left off.