-
-
Notifications
You must be signed in to change notification settings - Fork 50.5k
Expand file tree
/
Copy pathsliding_window_maximum.py
More file actions
59 lines (47 loc) · 1.5 KB
/
sliding_window_maximum.py
File metadata and controls
59 lines (47 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from collections import deque
def sliding_window_maximum(nums: list[int], window_size: int) -> list[int]:
"""
Return a list of the maximum values in each sliding window of the given size.
This algorithm runs in O(n) time using a deque to keep track of useful elements.
Parameters
----------
nums : list[int]
The input list of integers.
window_size : int
The size of the sliding window.
Returns
-------
list[int]
A list containing the maximum of each sliding window.
Examples
--------
>>> sliding_window_maximum([1, 3, -1, -3, 5, 3, 6, 7], 3)
[3, 3, 5, 5, 6, 7]
>>> sliding_window_maximum([9, 11], 2)
[11]
>>> sliding_window_maximum([4, -2], 1)
[4, -2]
>>> sliding_window_maximum([], 3)
[]
>>> sliding_window_maximum([1, 2, 3], 0)
[]
Reference
---------
https://en.wikipedia.org/wiki/Sliding_window_protocol
"""
if not nums or window_size <= 0:
return []
dq: deque[int] = deque()
result: list[int] = []
for i, num in enumerate(nums):
# Remove indices that are out of the current window
while dq and dq[0] <= i - window_size:
dq.popleft()
# Remove smaller values as they are not useful
while dq and nums[dq[-1]] < num:
dq.pop()
dq.append(i)
# Add the current max to the result once the window is of size window_size
if i >= window_size - 1:
result.append(nums[dq[0]])
return result