Sliding Window Max

T: O(n)
Feedback

Sliding Window Max

Initializing sliding window...
Array (k=3)
1
0
3
1
-1
2
-3
3
5
4
3
5
6
6
7
7
Deque (front → back)
empty
Result
waiting for window to fill...
WindowNew ElementMaximum
SpeedNormal (500ms)
Parameters

{ array: number[], k: number }

Variables
windowEnd-1
k3
deque[]
max
result[]
1function maxSlidingWindow(nums, k):
2 deque = []
3 result = []
4 for i = 0 to n-1:
5 // remove elements outside window
6 while deque.front < i - k + 1:
7 deque.removeFront()
8 // maintain decreasing order
9 while deque not empty and nums[deque.back] <= nums[i]:
10 deque.removeBack()
11 deque.pushBack(i)
12 if i >= k - 1:
13 result.push(nums[deque.front])
14 return result
Output
Initializing sliding window...