-
Notifications
You must be signed in to change notification settings - Fork 21k
Expand file tree
/
Copy pathSlidingWindowMax.java
More file actions
32 lines (26 loc) · 956 Bytes
/
SlidingWindowMax.java
File metadata and controls
32 lines (26 loc) · 956 Bytes
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
import java.util.*;
public class SlidingWindowMaximum {
public static int[] maxSlidingWindow(int[] nums, int k) {
int n = nums.length;
int[] result = new int[n - k + 1];
PriorityQueue<int[]> maxHeap = new PriorityQueue<>((a, b) -> b[0] - a[0]); // Max heap based on value
for (int i = 0; i < n; i++) {
//add current
maxHeap.offer(new int[]{nums[i], i});
// remove elements outside the current window
while (maxHeap.peek()[1] <= i - k) {
maxHeap.poll();
}
// Store the max for the current window
if (i >= k - 1) {
result[i - k + 1] = maxHeap.peek()[0];
}
}
return result;
}
public static void main(String[] args) {
int[] nums = {1, 3, -1, -3, 5, 3, 6, 7};
int k = 3;
System.out.println(Arrays.toString(maxSlidingWindow(nums, k)));
}
}