Sliding Window Maximum
HardAcc. 62.4%
+60 XP 30
Monotonic Dominance
To find the max in O(n), use a Deque to store indices. Maintain the deque so that the elements are in descending order.
The Assignment
Your function receives nums and k.
- Iterate through
nums. - Remove indices from the front that are out of the window (
< i - k + 1). - Remove indices from the back whose values are smaller than the current value (
nums[i]). - Add current index to back.
- If window is full, print
nums[deque[0]].
01EXAMPLE 1
Input
nums=[1,3,-1,-3,5,3,6,7], k=3Output
3
3
5
5
6
7Explanation: Current peaks for each window.
Constraints
- Must solve in O(n) time.
Data StructuresQueueSliding Window
JavaScriptSystem handles I/O — write your function only
Loading...
1 Hidden
Input Arguments
nums[1,3,-1,-3,5,3,6,7]
k3
Expected Output
3 3 5 5 6 7
Click RUN to test your solution