Algobase
Problems
Get Premium
Pricing
Problems
/
246. Sliding Window Max (Deque)
Prev
Next
Visualizer
Problem
Solution
Code
Standard Case (k=3)
Large Window (k=7)
Single Element
⌥
Visualizer will appear here
Problem
Solution
Code
1
def max_sliding_window(nums, k):
2
q = []
3
res = []
4
for i, num in enumerate(nums):
5
while q and nums[q[-1]] < num: q.pop()
6
q.append(i)
7
if q[0] == i - k: q.pop(0)
8
if i >= k - 1: res.append(nums[q[0]])
9
return res
Visualizer
Standard Case (k=3)
Large Window (k=7)
Single Element
⌥
Visualizer will appear here