Skip to content

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

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions data_structures/queue/Monotonic_Queue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""

Check failure on line 1 in data_structures/queue/Monotonic_Queue.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N999)

data_structures/queue/Monotonic_Queue.py:1:1: N999 Invalid module name: 'Monotonic_Queue'
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.

Check failure on line 3 in data_structures/queue/Monotonic_Queue.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

data_structures/queue/Monotonic_Queue.py:3:89: E501 Line too long (91 > 88)
"""

from collections import deque


class MonotonicQueue:
def __init__(self):

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:

self.deque = deque()

def push(self, value):

Choose a reason for hiding this comment

The 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 data_structures/queue/Monotonic_Queue.py, please provide doctest for the function push

Please provide return type hint for the function: push. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: value

while self.deque and self.deque[-1] < value:
self.deque.pop()
self.deque.append(value)

def max(self):

Choose a reason for hiding this comment

The 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 data_structures/queue/Monotonic_Queue.py, please provide doctest for the function max

Please provide return type hint for the function: max. If the function does not return a value, please provide the type hint as: def function() -> None:

return self.deque[0]

def pop(self, value):

Choose a reason for hiding this comment

The 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 data_structures/queue/Monotonic_Queue.py, please provide doctest for the function pop

Please provide return type hint for the function: pop. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: value

if self.deque and self.deque[0] == value:
self.deque.popleft()


def sliding_window_max(nums, k):

Choose a reason for hiding this comment

The 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 data_structures/queue/Monotonic_Queue.py, please provide doctest for the function sliding_window_max

Please provide return type hint for the function: sliding_window_max. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: nums

Please provide descriptive name for the parameter: k

Please provide type hint for the parameter: k

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
Loading