-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprob1.java
More file actions
32 lines (29 loc) · 1.01 KB
/
prob1.java
File metadata and controls
32 lines (29 loc) · 1.01 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
// Find Maximum in Sliding Window
// In this challenge, we must find the maximum value in a window.
//
// Given a large array of integers and a window of size w, find the current maximum value in the window as the window slides through the entire array.
//
// Consider the array below. Let’s try to find all maximums for a window size = 3.
//
import java.util.PriorityQueue;
import java.util.Comparator;
public class prob1{
public static void main(String[] args) {
int[] arr = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int window_size = 3;
Comparator<Integer> Comparator = new Comparator<Integer>(){
public int compare(Integer a, Integer b ){
return b-a;
}
};
PriorityQueue<Integer> queue = new PriorityQueue<Integer>(Comparator);
for (int i = 0; i < arr.length; i++ ) {
if (queue.size() >= window_size){
System.out.print(queue.peek() + " ");
queue.remove(arr[i - window_size]);
}
queue.add(arr[i]);
}
System.out.print(queue.peek());
}
}