Skip to content

Commit 4793aec

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 370e495 commit 4793aec

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

data_structures/queue/Monotonic_Queue.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
"""
2-
A Monotonic Queue is a data structure that supports efficient insertion, deletion,
2+
A Monotonic Queue is a data structure that supports efficient insertion, deletion,
33
and retrieval of elements in a specific order, typically in increasing or decreasing order.
44
"""
55

66
from collections import deque
77

8+
89
class MonotonicQueue:
910
def __init__(self):
1011
self.deque = deque()
@@ -21,14 +22,15 @@ def pop(self, value):
2122
if self.deque and self.deque[0] == value:
2223
self.deque.popleft()
2324

25+
2426
def sliding_window_max(nums, k):
2527
if not nums or k == 0:
2628
return []
2729
if k >= len(nums):
2830
return [max(nums)]
2931
if k == 1:
3032
return nums
31-
33+
3234
q = MonotonicQueue()
3335
result = []
3436
for i in range(len(nums)):
@@ -40,9 +42,10 @@ def sliding_window_max(nums, k):
4042
q.pop(nums[i - k + 1])
4143
return result
4244

45+
4346
# Test cases
44-
print(sliding_window_max([], 3)) # Edge case: Empty list
45-
print(sliding_window_max([1, 2], 3)) # Edge case: k > len(nums)
46-
print(sliding_window_max([1, 3, 2, 5, 4], 1)) # Edge case: k == 1
47-
print(sliding_window_max([1, 3, 2, 5, 4], 5)) # Edge case: k == len(nums)
47+
print(sliding_window_max([], 3)) # Edge case: Empty list
48+
print(sliding_window_max([1, 2], 3)) # Edge case: k > len(nums)
49+
print(sliding_window_max([1, 3, 2, 5, 4], 1)) # Edge case: k == 1
50+
print(sliding_window_max([1, 3, 2, 5, 4], 5)) # Edge case: k == len(nums)
4851
print(sliding_window_max([8, 5, 10, 7, 9, 4, 15, 12, 90, 13], 3)) # Normal case

0 commit comments

Comments
 (0)