-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOffer41.java
More file actions
49 lines (44 loc) · 1.49 KB
/
Offer41.java
File metadata and controls
49 lines (44 loc) · 1.49 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package lcof;
import java.util.PriorityQueue;
/**
* Offer 41. 数据流中的中位数
* 与中位数有关的题目,应该想到对顶堆
*
* @author LBW
*/
public class Offer41 {
private PriorityQueue<Integer> maxHeap; // 大顶堆
private PriorityQueue<Integer> minHeap; // 小顶堆
/**
* initialize your data structure here.
*/
public Offer41() {
maxHeap = new PriorityQueue<>((i1, i2) -> i2 - i1);
minHeap = new PriorityQueue<Integer>();
}
public void addNum(int num) {
if (maxHeap.size() == minHeap.size()) { // 需要往 bigHeap 里添加元素
// 因为这里要保证两个堆的大小性质,所以我们不能直接往 bigHeap 添加 num,而是往 smallHeap 里添加 num,然后将 smallHeap 的堆顶加入 bigHeap
if (minHeap.size() > 0 && minHeap.peek() < num) {
maxHeap.add(minHeap.poll());
minHeap.add(num);
} else {
maxHeap.add(num);
}
} else { // 需要往 smallHeap 里添加元素
if (maxHeap.size() > 0 && maxHeap.peek() > num) {
minHeap.add(maxHeap.poll());
maxHeap.add(num);
} else {
minHeap.add(num);
}
}
}
public double findMedian() {
if (maxHeap.size() == minHeap.size()) {
return (maxHeap.peek() + minHeap.peek()) / 2.0;
} else {
return maxHeap.peek();
}
}
}