forked from TellH/AlgorithmStudy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShiftWindow.java
48 lines (42 loc) · 1.23 KB
/
ShiftWindow.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
package SwordToOffer;
import java.util.ArrayDeque;
import java.util.ArrayList;
/**
* Created by tlh on 2017/4/1.
*/
public class ShiftWindow {
private ArrayDeque<Integer> q;
public ArrayList<Integer> maxInWindows(int[] num, int size) {
ArrayList<Integer> result = new ArrayList<>();
if (num == null || num.length < size || size == 0) return result;
int i = 0, j = 0;
q = new ArrayDeque<>();
for (; j <= size - 1; j++) {
addToQueue(num[j]);
}
result.add(q.peekFirst());
while (j <= num.length - 1) {
if (num[i++] == q.peekFirst()) {
q.pollFirst();
}
addToQueue(num[j++]);
result.add(q.peekFirst());
}
return result;
}
private void addToQueue(int e) {
if (q.isEmpty())
q.offer(e);
else if (q.peekLast() <= e) {
do {
q.pollLast();
} while (!q.isEmpty() && q.peekLast() < e);
q.offer(e);
} else {
q.offer(e);
}
}
public static void main(String[] args) {
System.out.println(new ShiftWindow().maxInWindows(new int[]{10, 14, 12, 11}, 4).toString());
}
}