Skip to content

Commit 049aba8

Browse files
Merge branch 'master' into Feature-slidingWindowUpdated
2 parents c7b61e6 + 04e421b commit 049aba8

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

DIRECTORY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@
207207
* [PriorityQueues](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/queues/PriorityQueues.java)
208208
* [Queue](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/queues/Queue.java)
209209
* [QueueByTwoStacks](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/queues/QueueByTwoStacks.java)
210+
* [SlidingWindowMaximum](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/queues/SlidingWindowMaximum.java)
210211
* [TokenBucket](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/queues/TokenBucket.java)
211212
* stacks
212213
* [NodeStack](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/stacks/NodeStack.java)
@@ -882,6 +883,7 @@
882883
* [PriorityQueuesTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/queues/PriorityQueuesTest.java)
883884
* [QueueByTwoStacksTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/queues/QueueByTwoStacksTest.java)
884885
* [QueueTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/queues/QueueTest.java)
886+
* [SlidingWindowMaximumTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/queues/SlidingWindowMaximumTest.java)
885887
* [TokenBucketTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/queues/TokenBucketTest.java)
886888
* stacks
887889
* [NodeStackTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/stacks/NodeStackTest.java)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.thealgorithms.datastructures.queues;
2+
3+
import java.util.Deque;
4+
import java.util.LinkedList;
5+
6+
/**
7+
* The {@code SlidingWindowMaximum} class provides a method to efficiently compute
8+
* the maximum element within every sliding window of size {@code k} in a given array.
9+
*
10+
* <p>The algorithm uses a deque to maintain the indices of useful elements within
11+
* the current sliding window. The time complexity of this approach is O(n) since
12+
* each element is processed at most twice.
13+
*
14+
* @author Hardvan
15+
*/
16+
public final class SlidingWindowMaximum {
17+
private SlidingWindowMaximum() {
18+
}
19+
20+
/**
21+
* Returns an array of the maximum values for each sliding window of size {@code k}.
22+
* <p>If {@code nums} has fewer elements than {@code k}, the result will be an empty array.
23+
* <p>Example:
24+
* <pre>
25+
* Input: nums = [1, 3, -1, -3, 5, 3, 6, 7], k = 3
26+
* Output: [3, 3, 5, 5, 6, 7]
27+
* </pre>
28+
*
29+
* @param nums the input array of integers
30+
* @param k the size of the sliding window
31+
* @return an array containing the maximum element for each sliding window
32+
*/
33+
public static int[] maxSlidingWindow(int[] nums, int k) {
34+
int n = nums.length;
35+
if (n < k || k == 0) {
36+
return new int[0];
37+
}
38+
39+
int[] result = new int[n - k + 1];
40+
Deque<Integer> deque = new LinkedList<>();
41+
for (int i = 0; i < n; i++) {
42+
// Remove elements from the front of the deque if they are out of the current window
43+
if (!deque.isEmpty() && deque.peekFirst() < i - k + 1) {
44+
deque.pollFirst();
45+
}
46+
47+
// Remove elements from the back if they are smaller than the current element
48+
while (!deque.isEmpty() && nums[deque.peekLast()] < nums[i]) {
49+
deque.pollLast();
50+
}
51+
52+
// Add the current element's index to the deque
53+
deque.offerLast(i);
54+
55+
// Store the maximum element for the current window (starting from the k-1th element)
56+
if (i >= k - 1) {
57+
result[i - k + 1] = nums[deque.peekFirst()];
58+
}
59+
}
60+
61+
return result;
62+
}
63+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.thealgorithms.datastructures.queues;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
5+
import java.util.stream.Stream;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.Arguments;
8+
import org.junit.jupiter.params.provider.MethodSource;
9+
10+
public class SlidingWindowMaximumTest {
11+
12+
@ParameterizedTest
13+
@MethodSource("provideTestCases")
14+
public void testMaxSlidingWindow(int[] nums, int k, int[] expected) {
15+
assertArrayEquals(expected, SlidingWindowMaximum.maxSlidingWindow(nums, k));
16+
}
17+
18+
private static Stream<Arguments> provideTestCases() {
19+
return Stream.of(
20+
// Test case 1: Example from the problem statement
21+
Arguments.of(new int[] {1, 3, -1, -3, 5, 3, 6, 7}, 3, new int[] {3, 3, 5, 5, 6, 7}),
22+
23+
// Test case 2: All elements are the same
24+
Arguments.of(new int[] {4, 4, 4, 4, 4}, 2, new int[] {4, 4, 4, 4}),
25+
26+
// Test case 3: Window size equals the array length
27+
Arguments.of(new int[] {2, 1, 5, 3, 6}, 5, new int[] {6}),
28+
29+
// Test case 4: Single element array with window size 1
30+
Arguments.of(new int[] {7}, 1, new int[] {7}),
31+
32+
// Test case 5: Window size larger than the array length
33+
Arguments.of(new int[] {1, 2, 3}, 4, new int[] {}),
34+
35+
// Test case 6: Decreasing sequence
36+
Arguments.of(new int[] {9, 8, 7, 6, 5, 4}, 3, new int[] {9, 8, 7, 6}),
37+
38+
// Test case 7: Increasing sequence
39+
Arguments.of(new int[] {1, 2, 3, 4, 5}, 2, new int[] {2, 3, 4, 5}),
40+
41+
// Test case 8: k is zero
42+
Arguments.of(new int[] {1, 3, -1, -3, 5, 3, 6, 7}, 0, new int[] {}),
43+
44+
// Test case 9: Array with negative numbers
45+
Arguments.of(new int[] {-4, -2, -5, -1, -3}, 3, new int[] {-2, -1, -1}),
46+
47+
// Test case 10: Empty array
48+
Arguments.of(new int[] {}, 3, new int[] {}));
49+
}
50+
}

0 commit comments

Comments
 (0)