-
Notifications
You must be signed in to change notification settings - Fork 277
/
FindMedianfromDataStream.java
48 lines (38 loc) · 1.05 KB
/
FindMedianfromDataStream.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
// No<0 (TreeMap left), 0-100[array], 101..(TreeMap right)
// TC : O(klogn)
// SC : O(k)
class MedianFinder {
private PriorityQueue<Integer> maxHeap = null;
private PriorityQueue<Integer> minHeap = null;
/** initialize your data structure here. */
public MedianFinder() {
maxHeap = new PriorityQueue<Integer>((a,b) -> (b-a));
minHeap = new PriorityQueue<Integer>((a,b) -> (a-b));
}
public void addNum(int num) {
if(maxHeap.size() == 0 || maxHeap.peek()>=num){
maxHeap.offer(num);
} else {
minHeap.offer(num);
}
balance();
}
public double findMedian() {
if(maxHeap.size()> minHeap.size()){
return maxHeap.peek();
} else if(maxHeap.size()< minHeap.size()) {
return minHeap.peek();
} else{
// maxHeap == minHeap size
return (maxHeap.peek() + minHeap.peek())/2.0;
}
}
private void balance(){
if(maxHeap.size() - minHeap.size() >1){
minHeap.offer(maxHeap.poll());
}
else if(minHeap.size() - maxHeap.size() >1){
maxHeap.offer(minHeap.poll());
}
}
}