-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution59.java
46 lines (40 loc) · 1.16 KB
/
Solution59.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
package com.usher.algorithm.offer;
import java.util.ArrayList;
import java.util.PriorityQueue;
/**
* @Author: Usher
* @Description:
*/
public class Solution59 {
public ArrayList<Integer> maxInWindows(int [] num, int size)
{
/*//常规
if (size == 0)
return new ArrayList<>();
ArrayList<Integer> list = new ArrayList<>();
for (int i =0;i < num.length-size;i++){
int tmp = num[i];
for (int j = i;j < i+size;j++){
if (tmp < num[j]){
tmp = num[j];
}
}
list.add(tmp);
}
return list;*/
//优先队列
ArrayList<Integer> list = new ArrayList<>();
PriorityQueue<Integer> heap = new PriorityQueue<>(((o1, o2) -> o2-o1));
if (size > num.length || size < 1)
return list;
for (int i = 0; i < size; i++)
heap.add(num[i]);
list.add(heap.peek());
for (int i = 1, j = i + size - 1; j < num.length; i++, j++) {
heap.remove(num[i - 1]);
heap.add(num[j]);
list.add(heap.peek());
}
return list;
}
}