-
-
Notifications
You must be signed in to change notification settings - Fork 49.9k
Add sliding window maximum using monotonic deque #14133
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
Open
P09s
wants to merge
3
commits into
TheAlgorithms:master
Choose a base branch
from
P09s:feat/add-sliding-window-maximum
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+58
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| from collections import deque | ||
|
|
||
|
|
||
| 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 | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
What's the difference between this and maths/max_sum_sliding_window.py?
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.
Hi @poyea, thanks for the review!
Great question — these two are related (both use sliding window idea), but they solve completely different problems:
maths/max_sum_sliding_window.pyfinds the maximum sum of any single window of sizek→ returns one integer (the largest possible sum among all k-sized subarrays). It uses a simple running sum technique.My PR adds
other/sliding_window_maximum.pywhich finds the maximum element in every sliding window of sizek→ returns a list of max values (one per window position). This is the classic "Sliding Window Maximum" problem (LeetCode 239), solved efficiently with a monotonic deque to track the current maximum in O(1) per step.Example to show the difference:
Input: nums = [1, 3, -1, -3, 5, 3, 6, 7], k = 3
They have different outputs, different goals, and use different internal logic (deque vs simple sum update). No overlap/duplication — this adds a popular, missing hard problem from LeetCode/competitive programming.
Happy to make any adjustments or add more comments if needed!