-
-
Notifications
You must be signed in to change notification settings - Fork 46.7k
feat: implement Monotonic Queue and sliding window maximum function with test cases #12226
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1209ace
370e495
4793aec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
""" | ||
A Monotonic Queue is a data structure that supports efficient insertion, deletion, | ||
and retrieval of elements in a specific order, typically in increasing or decreasing order. | ||
""" | ||
|
||
from collections import deque | ||
|
||
|
||
class MonotonicQueue: | ||
def __init__(self): | ||
self.deque = deque() | ||
|
||
def push(self, value): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file Please provide return type hint for the function: Please provide type hint for the parameter: |
||
while self.deque and self.deque[-1] < value: | ||
self.deque.pop() | ||
self.deque.append(value) | ||
|
||
def max(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file Please provide return type hint for the function: |
||
return self.deque[0] | ||
|
||
def pop(self, value): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file Please provide return type hint for the function: Please provide type hint for the parameter: |
||
if self.deque and self.deque[0] == value: | ||
self.deque.popleft() | ||
|
||
|
||
def sliding_window_max(nums, k): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file Please provide return type hint for the function: Please provide type hint for the parameter: Please provide descriptive name for the parameter: Please provide type hint for the parameter: |
||
if not nums or k == 0: | ||
return [] | ||
if k >= len(nums): | ||
return [max(nums)] | ||
if k == 1: | ||
return nums | ||
|
||
q = MonotonicQueue() | ||
result = [] | ||
for i in range(len(nums)): | ||
if i < k - 1: | ||
q.push(nums[i]) | ||
else: | ||
q.push(nums[i]) | ||
result.append(q.max()) | ||
q.pop(nums[i - k + 1]) | ||
return result | ||
|
||
|
||
# Test cases | ||
print(sliding_window_max([], 3)) # Edge case: Empty list | ||
print(sliding_window_max([1, 2], 3)) # Edge case: k > len(nums) | ||
print(sliding_window_max([1, 3, 2, 5, 4], 1)) # Edge case: k == 1 | ||
print(sliding_window_max([1, 3, 2, 5, 4], 5)) # Edge case: k == len(nums) | ||
print(sliding_window_max([8, 5, 10, 7, 9, 4, 15, 12, 90, 13], 3)) # Normal case |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide return type hint for the function:
__init__
. If the function does not return a value, please provide the type hint as:def function() -> None: