-
Notifications
You must be signed in to change notification settings - Fork 20.5k
Added Maximum Sliding Window algorithm using deque for efficient O(n) solution. #5848
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3f4f359
Add Maximum Sliding Window algorithm using deque for efficient O(n) s…
pranayh24 d62fe55
Formatted MaximumSlidingWindow.java using clang-format
pranayh24 bf74f4f
Fixed Checkstyle issue by adding braces to for loop
pranayh24 5fc39c0
Merge branch 'master' into master
pranayh24 beaec8a
Merge branch 'master' into master
pranayh24 1f13d84
Add tests for MaximumSlidingWindow
pranayh24 5e3ddc3
Added test function for MaximumSlidingWindow
pranayh24 1aba467
Merge remote-tracking branch 'origin/master'
pranayh24 b5305ff
Fixed formatting issues
pranayh24 f3f705a
Fix method names to conform to Checkstyle standards
pranayh24 2d084f4
Merge branch 'master' into master
siriak 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
69 changes: 69 additions & 0 deletions
69
src/main/java/com/thealgorithms/others/MaximumSlidingWindow.java
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,69 @@ | ||
package com.thealgorithms.others; | ||
|
||
import java.util.ArrayDeque; | ||
import java.util.Deque; | ||
|
||
/** | ||
* Maximum Sliding Window Algorithm | ||
* | ||
* This algorithm finds the maximum element in each sliding window of size k | ||
* in a given array of integers. It uses a deque (double-ended queue) to | ||
* efficiently keep track of potential maximum values in the current window. | ||
* | ||
* Time Complexity: O(n), where n is the number of elements in the input array | ||
* Space Complexity: O(k), where k is the size of the sliding window | ||
*/ | ||
|
||
public class MaximumSlidingWindow { | ||
|
||
/** | ||
* Finds the maximum values in each sliding window of size k. | ||
* | ||
* @param nums The input array of integers | ||
* @param windowSize The size of the sliding window | ||
* @return An array of integers representing the maximums in each window | ||
*/ | ||
public int[] maxSlidingWindow(int[] nums, int windowSize) { | ||
if (nums == null || nums.length == 0 || windowSize <= 0 || windowSize > nums.length) { | ||
return new int[0]; // Handle edge cases | ||
} | ||
|
||
int[] result = new int[nums.length - windowSize + 1]; | ||
Deque<Integer> deque = new ArrayDeque<>(); | ||
|
||
for (int currentIndex = 0; currentIndex < nums.length; currentIndex++) { | ||
|
||
// Remove the first element if it's outside the current window | ||
if (!deque.isEmpty() && deque.peekFirst() == currentIndex - windowSize) { | ||
deque.pollFirst(); | ||
} | ||
|
||
// Remove all elements smaller than the current element from the end | ||
while (!deque.isEmpty() && nums[deque.peekLast()] < nums[currentIndex]) { | ||
deque.pollLast(); | ||
} | ||
|
||
// Add the current element's index to the deque | ||
deque.offerLast(currentIndex); | ||
|
||
// If we have processed at least k elements, add to result | ||
if (currentIndex >= windowSize - 1) { | ||
result[currentIndex - windowSize + 1] = nums[deque.peekFirst()]; | ||
} | ||
} | ||
return result; | ||
} | ||
public static void main(String[] args) { | ||
MaximumSlidingWindow msw = new MaximumSlidingWindow(); | ||
int[] nums = {1, 3, -1, -3, 5, 3, 6, 7}; | ||
int k = 3; | ||
|
||
// Calculate the maximum sliding window | ||
int[] result = msw.maxSlidingWindow(nums, k); | ||
|
||
// Print the result | ||
for (int num : result) { | ||
System.out.print(num + " "); | ||
} | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.