From 1209aceb03c665375b8d52b9d3c53cd9adfcfd8c Mon Sep 17 00:00:00 2001 From: Aditya <161931434+AdityaPatadiya@users.noreply.github.com> Date: Mon, 21 Oct 2024 13:03:24 +0530 Subject: [PATCH 1/2] feat: implement Monotonic Queue and sliding window maximum function with test cases --- data_structures/queue/Monotonic_Queue.py | 48 ++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 data_structures/queue/Monotonic_Queue.py diff --git a/data_structures/queue/Monotonic_Queue.py b/data_structures/queue/Monotonic_Queue.py new file mode 100644 index 000000000000..d45c2358bc00 --- /dev/null +++ b/data_structures/queue/Monotonic_Queue.py @@ -0,0 +1,48 @@ +""" +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): + while self.deque and self.deque[-1] < value: + self.deque.pop() + self.deque.append(value) + + def max(self): + return self.deque[0] + + def pop(self, value): + if self.deque and self.deque[0] == value: + self.deque.popleft() + +def sliding_window_max(nums, 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 From 4793aec30e43616d07c41022c6ba40b574e014f8 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 21 Oct 2024 14:55:22 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/queue/Monotonic_Queue.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/data_structures/queue/Monotonic_Queue.py b/data_structures/queue/Monotonic_Queue.py index d45c2358bc00..c645b9a4d779 100644 --- a/data_structures/queue/Monotonic_Queue.py +++ b/data_structures/queue/Monotonic_Queue.py @@ -1,10 +1,11 @@ """ -A Monotonic Queue is a data structure that supports efficient insertion, deletion, +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() @@ -21,6 +22,7 @@ def pop(self, value): if self.deque and self.deque[0] == value: self.deque.popleft() + def sliding_window_max(nums, k): if not nums or k == 0: return [] @@ -28,7 +30,7 @@ def sliding_window_max(nums, k): return [max(nums)] if k == 1: return nums - + q = MonotonicQueue() result = [] for i in range(len(nums)): @@ -40,9 +42,10 @@ def sliding_window_max(nums, k): 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([], 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