-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSlidingWindowMaximum.java
More file actions
39 lines (34 loc) · 1.28 KB
/
SlidingWindowMaximum.java
File metadata and controls
39 lines (34 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package solutions;
import java.util.*;
// [Problem] https://leetcode.com/problems/sliding-window-maximum
class SlidingWindowMaximum {
// Sliding window with Deque
// O(n) time, O(k) space
public int[] maxSlidingWindow(int[] nums, int k) {
int n = nums.length;
int[] max = new int[n - k + 1];
Deque<Integer> windowIndices = new ArrayDeque<>();
for (int i = 0; i < n; i++) {
int maxIndex = i - k + 1;
if (!windowIndices.isEmpty() && windowIndices.peek() < maxIndex) {
windowIndices.poll();
}
while (!windowIndices.isEmpty() && nums[windowIndices.peekLast()] < nums[i]) {
windowIndices.pollLast();
}
windowIndices.add(i);
if (maxIndex >= 0) {
max[maxIndex] = nums[windowIndices.peek()];
}
}
return max;
}
// Test
public static void main(String[] args) {
SlidingWindowMaximum solution = new SlidingWindowMaximum();
int[] input = {1, 3, -1, -3, 5, 3, 6, 7};
int[] expectedOutput = {3, 3, 5, 5, 6, 7};
int[] actualOutput = solution.maxSlidingWindow(input, 3);
System.out.println("Test passed? " + Arrays.equals(expectedOutput, actualOutput));
}
}