Skip to content

Commit 985c1f9

Browse files
authored
Add Maximum Sliding Window algorithm (TheAlgorithms#5848)
1 parent 3da16a7 commit 985c1f9

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.thealgorithms.others;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.Deque;
5+
6+
/**
7+
* Maximum Sliding Window Algorithm
8+
*
9+
* This algorithm finds the maximum element in each sliding window of size k
10+
* in a given array of integers. It uses a deque (double-ended queue) to
11+
* efficiently keep track of potential maximum values in the current window.
12+
*
13+
* Time Complexity: O(n), where n is the number of elements in the input array
14+
* Space Complexity: O(k), where k is the size of the sliding window
15+
*/
16+
17+
public class MaximumSlidingWindow {
18+
19+
/**
20+
* Finds the maximum values in each sliding window of size k.
21+
*
22+
* @param nums The input array of integers
23+
* @param windowSize The size of the sliding window
24+
* @return An array of integers representing the maximums in each window
25+
*/
26+
public int[] maxSlidingWindow(int[] nums, int windowSize) {
27+
if (nums == null || nums.length == 0 || windowSize <= 0 || windowSize > nums.length) {
28+
return new int[0]; // Handle edge cases
29+
}
30+
31+
int[] result = new int[nums.length - windowSize + 1];
32+
Deque<Integer> deque = new ArrayDeque<>();
33+
34+
for (int currentIndex = 0; currentIndex < nums.length; currentIndex++) {
35+
36+
// Remove the first element if it's outside the current window
37+
if (!deque.isEmpty() && deque.peekFirst() == currentIndex - windowSize) {
38+
deque.pollFirst();
39+
}
40+
41+
// Remove all elements smaller than the current element from the end
42+
while (!deque.isEmpty() && nums[deque.peekLast()] < nums[currentIndex]) {
43+
deque.pollLast();
44+
}
45+
46+
// Add the current element's index to the deque
47+
deque.offerLast(currentIndex);
48+
49+
// If we have processed at least k elements, add to result
50+
if (currentIndex >= windowSize - 1) {
51+
result[currentIndex - windowSize + 1] = nums[deque.peekFirst()];
52+
}
53+
}
54+
return result;
55+
}
56+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.thealgorithms.others;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
5+
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.Test;
7+
8+
class MaximumSlidingWindowTest {
9+
10+
MaximumSlidingWindow msw;
11+
int[] nums;
12+
int k;
13+
14+
@BeforeEach
15+
void setUp() {
16+
msw = new MaximumSlidingWindow(); // Initialize the MaximumSlidingWindow object
17+
}
18+
19+
// Test for a simple sliding window case
20+
@Test
21+
void testMaxSlidingWindowSimpleCase() {
22+
nums = new int[] {1, 3, -1, -3, 5, 3, 6, 7};
23+
k = 3;
24+
int[] expected = {3, 3, 5, 5, 6, 7};
25+
assertArrayEquals(expected, msw.maxSlidingWindow(nums, k));
26+
}
27+
28+
// Test when window size is 1 (output should be the array itself)
29+
@Test
30+
void testMaxSlidingWindowWindowSizeOne() {
31+
nums = new int[] {4, 2, 12, 11, -5};
32+
k = 1;
33+
int[] expected = {4, 2, 12, 11, -5};
34+
assertArrayEquals(expected, msw.maxSlidingWindow(nums, k));
35+
}
36+
37+
// Test when the window size is equal to the array length (output should be a single max element)
38+
@Test
39+
void testMaxSlidingWindowWindowSizeEqualsArrayLength() {
40+
nums = new int[] {4, 2, 12, 11, -5};
41+
k = nums.length;
42+
int[] expected = {12}; // Maximum of the entire array
43+
assertArrayEquals(expected, msw.maxSlidingWindow(nums, k));
44+
}
45+
46+
// Test when the input array is empty
47+
@Test
48+
void testMaxSlidingWindowEmptyArray() {
49+
nums = new int[] {};
50+
k = 3;
51+
int[] expected = {};
52+
assertArrayEquals(expected, msw.maxSlidingWindow(nums, k));
53+
}
54+
55+
// Test when the window size is larger than the array (should return empty)
56+
@Test
57+
void testMaxSlidingWindowWindowSizeLargerThanArray() {
58+
nums = new int[] {1, 2, 3};
59+
k = 5;
60+
int[] expected = {}; // Window size is too large, so no result
61+
assertArrayEquals(expected, msw.maxSlidingWindow(nums, k));
62+
}
63+
}

0 commit comments

Comments
 (0)