-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path295.cpp
More file actions
31 lines (28 loc) · 785 Bytes
/
295.cpp
File metadata and controls
31 lines (28 loc) · 785 Bytes
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
class MedianFinder {
public:
priority_queue<int> lowHeap;
priority_queue<int, vector<int>, greater<int>> highHeap;
MedianFinder() {
}
void addNum(int num) {
lowHeap.push(num);
highHeap.push(lowHeap.top());
lowHeap.pop();
if (highHeap.size() > lowHeap.size()) {
lowHeap.push(highHeap.top());
highHeap.pop();
}
}
double findMedian() {
if (lowHeap.size() > highHeap.size()) {
return lowHeap.top();
}
return ((double)lowHeap.top() + (double)highHeap.top()) / 2;
}
};
/**
* Your MedianFinder object will be instantiated and called as such:
* MedianFinder* obj = new MedianFinder();
* obj->addNum(num);
* double param_2 = obj->findMedian();
*/