-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStreamMedian.java
68 lines (55 loc) · 1.78 KB
/
StreamMedian.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
57
58
59
60
61
62
63
64
65
66
67
68
package org.offer.case64;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.PriorityQueue;
/**
* 面试题 64: 数据流中的中位数
* Created by tanc on 2017/7/12.
*/
public class StreamMedian {
/**
* 使用一个最大堆 和 一个最小堆,并保持它们的节点数只差不超过 1
*/
public static Integer methodOne(List<Integer> stream) {
Iterator<Integer> it = stream.iterator();
PriorityQueue<Integer> big = new PriorityQueue<>(Comparator.reverseOrder());
PriorityQueue<Integer> small = new PriorityQueue<>(Comparator.naturalOrder());
while (it.hasNext()) {
Integer data = it.next();
if (big.isEmpty() && small.isEmpty()) {
big.add(data);
} else {
Integer temp = big.peek();
if (temp.compareTo(data) > 0) {
big.add(data);
} else {
small.add(data);
}
}
check(big, small);
}
Integer result;
if (big.size() > small.size()) {
result = big.peek();
} else if (small.size() > big.size()) {
result = small.peek();
} else {
result = (big.peek() + small.peek()) / 2;
}
return result;
}
/**
* 平衡最大堆和最小堆
* 使最大堆和最小堆的节点数差不大于 1
*/
private static <E> void check(PriorityQueue<E> big, PriorityQueue<E> small) {
int bigCount = big.size();
int smallCount = small.size();
if (bigCount - smallCount > 1) {
small.add(big.poll());
} else if (smallCount - bigCount > 1) {
big.add(small.poll());
}
}
}