From 0a6830adf02fce5485de340862b15baeaf7c202c Mon Sep 17 00:00:00 2001 From: MAHY U DEEN SHAHID Date: Wed, 22 Oct 2025 00:32:45 +0500 Subject: [PATCH 1/2] Add sliding_window_maximum function Implement sliding window maximum algorithm using deque. --- .../arrays/sliding_window_maximum.py | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 data_structures/arrays/sliding_window_maximum.py diff --git a/data_structures/arrays/sliding_window_maximum.py b/data_structures/arrays/sliding_window_maximum.py new file mode 100644 index 000000000000..213328dcdf74 --- /dev/null +++ b/data_structures/arrays/sliding_window_maximum.py @@ -0,0 +1,57 @@ +from collections import deque +from typing import List + + +def sliding_window_maximum(nums: List[int], k: int) -> List[int]: + """ + Return a list of the maximum values in each sliding window of size k. + + This algorithm runs in O(n) time using a deque to keep track of useful elements. + + Parameters + ---------- + nums : List[int] + The input list of integers. + k : int + The window size. + + Returns + ------- + List[int] + A list containing the maximum of each sliding window. + + 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([4, -2], 1) + [4, -2] + >>> sliding_window_maximum([], 3) + [] + >>> sliding_window_maximum([1,2,3], 0) + [] + """ + if not nums or k <= 0: + return [] + + dq: deque[int] = deque() + result: List[int] = [] + + for i, num in enumerate(nums): + # Remove indices that are out of the current window + while dq and dq[0] <= i - k: + dq.popleft() + + # Remove smaller values as they are not useful + while dq and nums[dq[-1]] < num: + dq.pop() + + dq.append(i) + + # Add the current max to the result once the window is of size k + if i >= k - 1: + result.append(nums[dq[0]]) + + return result From 2ac3757f5ef7f01286baaf1c6f025095dcd5098f Mon Sep 17 00:00:00 2001 From: MAHY U DEEN SHAHID Date: Wed, 22 Oct 2025 00:43:35 +0500 Subject: [PATCH 2/2] Refactor sliding_window_maximum function signature Updated type hints and parameter names for clarity. --- .../arrays/sliding_window_maximum.py | 32 ++++++++++--------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/data_structures/arrays/sliding_window_maximum.py b/data_structures/arrays/sliding_window_maximum.py index 213328dcdf74..eaa44c3891c3 100644 --- a/data_structures/arrays/sliding_window_maximum.py +++ b/data_structures/arrays/sliding_window_maximum.py @@ -1,28 +1,26 @@ from collections import deque -from typing import List -def sliding_window_maximum(nums: List[int], k: int) -> List[int]: +def sliding_window_maximum(nums: list[int], window_size: int) -> list[int]: """ - Return a list of the maximum values in each sliding window of size k. - + Return a list of the maximum values in each sliding window of the given size. This algorithm runs in O(n) time using a deque to keep track of useful elements. Parameters ---------- - nums : List[int] + nums : list[int] The input list of integers. - k : int - The window size. + window_size : int + The size of the sliding window. Returns ------- - List[int] + list[int] A list containing the maximum of each sliding window. Examples -------- - >>> sliding_window_maximum([1,3,-1,-3,5,3,6,7], 3) + >>> 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] @@ -30,18 +28,22 @@ def sliding_window_maximum(nums: List[int], k: int) -> List[int]: [4, -2] >>> sliding_window_maximum([], 3) [] - >>> sliding_window_maximum([1,2,3], 0) + >>> sliding_window_maximum([1, 2, 3], 0) [] + + Reference + --------- + https://en.wikipedia.org/wiki/Sliding_window_protocol """ - if not nums or k <= 0: + if not nums or window_size <= 0: return [] dq: deque[int] = deque() - result: List[int] = [] + result: list[int] = [] for i, num in enumerate(nums): # Remove indices that are out of the current window - while dq and dq[0] <= i - k: + while dq and dq[0] <= i - window_size: dq.popleft() # Remove smaller values as they are not useful @@ -50,8 +52,8 @@ def sliding_window_maximum(nums: List[int], k: int) -> List[int]: dq.append(i) - # Add the current max to the result once the window is of size k - if i >= k - 1: + # Add the current max to the result once the window is of size window_size + if i >= window_size - 1: result.append(nums[dq[0]]) return result