From f8715831edb11fb7ab81147ae63f37fddad79f4f Mon Sep 17 00:00:00 2001 From: Parag Sharma Date: Mon, 12 Jan 2026 19:48:34 +0530 Subject: [PATCH 1/3] feat: add sliding window maximum using monotonic deque --- other/sliding_window_maximum.py | 59 +++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 other/sliding_window_maximum.py diff --git a/other/sliding_window_maximum.py b/other/sliding_window_maximum.py new file mode 100644 index 000000000000..4fd818e3fed2 --- /dev/null +++ b/other/sliding_window_maximum.py @@ -0,0 +1,59 @@ +from collections import deque +from typing import Deque, List + + +def sliding_window_maximum(numbers: List[int], window_size: int) -> List[int]: + """ + Return a list containing the maximum of each sliding window of size window_size. + + This implementation uses a monotonic deque to achieve O(n) time complexity. + + Args: + numbers: List of integers representing the input array. + window_size: Size of the sliding window (must be positive). + + Returns: + List of maximum values for each valid window. + + Raises: + ValueError: If window_size is not a positive integer. + + Time Complexity: O(n) - each element is added and removed at most once + Space Complexity: O(k) - deque stores at most window_size indices + + 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([], 3) + [] + >>> sliding_window_maximum([4, 2, 12, 3], 1) + [4, 2, 12, 3] + >>> sliding_window_maximum([1], 1) + [1] + """ + if window_size <= 0: + raise ValueError("Window size must be a positive integer") + if not numbers: + return [] + + result: List[int] = [] + index_deque: Deque[int] = deque() + + for current_index, current_value in enumerate(numbers): + # Remove the element which is out of this window + if index_deque and index_deque[0] == current_index - window_size: + index_deque.popleft() + + # Remove useless elements (smaller than current) from back + while index_deque and numbers[index_deque[-1]] < current_value: + index_deque.pop() + + index_deque.append(current_index) + + # Start adding to result once we have a full window + if current_index >= window_size - 1: + result.append(numbers[index_deque[0]]) + + return result \ No newline at end of file From ecf074dd3830f175d47dca9dd7f1569f520800f1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 12 Jan 2026 14:20:17 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- other/sliding_window_maximum.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/other/sliding_window_maximum.py b/other/sliding_window_maximum.py index 4fd818e3fed2..1c38e878bc58 100644 --- a/other/sliding_window_maximum.py +++ b/other/sliding_window_maximum.py @@ -56,4 +56,4 @@ def sliding_window_maximum(numbers: List[int], window_size: int) -> List[int]: if current_index >= window_size - 1: result.append(numbers[index_deque[0]]) - return result \ No newline at end of file + return result From e4f9d6d8cb190301a0c453c267a35027637f0b48 Mon Sep 17 00:00:00 2001 From: Parag Sharma Date: Mon, 12 Jan 2026 20:18:17 +0530 Subject: [PATCH 3/3] feat: add sliding window maximum using monotonic deque --- other/sliding_window_maximum.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/other/sliding_window_maximum.py b/other/sliding_window_maximum.py index 1c38e878bc58..1c2c3c8e37e6 100644 --- a/other/sliding_window_maximum.py +++ b/other/sliding_window_maximum.py @@ -1,8 +1,7 @@ from collections import deque -from typing import Deque, List -def sliding_window_maximum(numbers: List[int], window_size: int) -> List[int]: +def sliding_window_maximum(numbers: list[int], window_size: int) -> list[int]: """ Return a list containing the maximum of each sliding window of size window_size. @@ -38,8 +37,8 @@ def sliding_window_maximum(numbers: List[int], window_size: int) -> List[int]: if not numbers: return [] - result: List[int] = [] - index_deque: Deque[int] = deque() + result: list[int] = [] + index_deque: deque[int] = deque() for current_index, current_value in enumerate(numbers): # Remove the element which is out of this window