-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMedianOfAStream.java
49 lines (42 loc) · 1.52 KB
/
MedianOfAStream.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
/*https://leetcode.com/problems/find-median-from-data-stream/*/
class MedianFinder {
PriorityQueue<Integer> minHeap, maxHeap;
public MedianFinder() {
minHeap = new PriorityQueue<Integer>();
maxHeap = new PriorityQueue<Integer>(Collections.reverseOrder());
}
public void addNum(int num) {
//add the first element to the minheap
if (minHeap.isEmpty() && maxHeap.isEmpty())
minHeap.add(num);
//for rest of the elements
else
{
//if the element is greater than the root of the minheap
if (num > minHeap.peek())
//add it to the minheap
minHeap.add(num);
//otherwise
else
//add it to the maxheap
maxHeap.add(num);
}
//balance the heaps
rebalance();
}
private void rebalance()
{
//if the size gap is more than 1, move one element from the larger heap to the smaller heap
if (minHeap.size()-maxHeap.size() == 2)
maxHeap.add(minHeap.poll());
else if (maxHeap.size()-minHeap.size() == 2)
minHeap.add(maxHeap.poll());
}
public double findMedian() {
//if sizes are same then pick both roots and take average
if (minHeap.size() == maxHeap.size())
return ((double)(minHeap.peek()+maxHeap.peek()))/2;
//otherwise root of the larger heap will be the answer
return minHeap.size()-maxHeap.size() == 1 ? minHeap.peek() : maxHeap.peek();
}
}