-
Notifications
You must be signed in to change notification settings - Fork 0
/
a_175_Sliding_Window_Maximun.java
56 lines (41 loc) · 1.35 KB
/
a_175_Sliding_Window_Maximun.java
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.util.* ;
public class a_175_Sliding_Window_Maximun {
static class Pair implements Comparable<Pair> {
int val ;
int idx ;
public Pair(int val, int idx ){
this.val = val ;
this.idx = idx ;
}
@Override
public int compareTo(Pair p2){
// ascending order
// return this.val - p2.val ;
// descending order
return p2.val - this.val ;
}
}
public static void main(String[] args) {
int arr[] = { 1, 3, -1, -3, 5, 3, 6, 7 } ;
int k = 3 ; // Window size
int ans[] = new int[arr.length-k+1] ; // n-k+1
PriorityQueue<Pair> pq = new PriorityQueue<>() ;
// First Window
for(int i=0; i<k ; i++){
pq.add(new Pair(arr[i], i) ) ;
}
ans[0] = pq.peek().val ;
for(int i=k ; i<arr.length ; i++){
while(pq.size() > 0 && pq.peek().idx <= (i-k) ) {
pq.remove() ;
}
pq.add(new Pair(arr[i], i) ) ;
ans[i-k+1] = pq.peek().val ;
}
// print result or answer
for(int i=0; i<ans.length ; i++){
System.out.print(ans[i]+" ");
}
System.out.println();
}
}