-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindMedianfromDataStream.java
More file actions
46 lines (35 loc) · 1.26 KB
/
FindMedianfromDataStream.java
File metadata and controls
46 lines (35 loc) · 1.26 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
class MedianFinder {
//maxheap for the smaller half ie [1 2 3 4 5 6 7 8] so maxheap stores [1 2 3 4]
private PriorityQueue<Integer> max_heap;
//min heap will store [5 6 7 8]
private PriorityQueue<Integer> min_heap;
public MedianFinder() {
max_heap = new PriorityQueue<>((a, b) -> b-a);//lembda exp
min_heap = new PriorityQueue<>();
}
public void addNum(int num) {
if(max_heap.isEmpty() || num <= max_heap.peek())
max_heap.offer(num);
else
min_heap.offer(num);
//need to balance heap with atmost 1 extra ele allow in maxheap
if(max_heap.size() > min_heap.size() + 1)
min_heap.offer(max_heap.poll());
else if(min_heap.size() > max_heap.size())
max_heap.offer(min_heap.poll());
}
public double findMedian() {
//if size of array odd
if(max_heap.size() > min_heap.size())
return max_heap.peek();
//size of array even
else
return (max_heap.peek() + min_heap.peek()) / (double)2;
}
}
/**
* Your MedianFinder object will be instantiated and called as such:
* MedianFinder obj = new MedianFinder();
* obj.addNum(num);
* double param_2 = obj.findMedian();
*/