-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindMedianfromDataStream.cpp
More file actions
39 lines (34 loc) · 1018 Bytes
/
findMedianfromDataStream.cpp
File metadata and controls
39 lines (34 loc) · 1018 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
32
33
34
35
36
37
38
39
// Source: https://leetcode.com/problems/find-median-from-data-stream/
// Author: Miao Zhang
// Date: 2021-01-31
class MedianFinder {
public:
priority_queue<int, vector<int>, less<int>> max_heap;
priority_queue<int, vector<int>, greater<int>> min_heap;
public:
/** initialize your data structure here. */
MedianFinder() {
}
void addNum(int num) {
min_heap.push(num);
max_heap.push(min_heap.top());
min_heap.pop();
if (max_heap.size() > min_heap.size()) {
min_heap.push(max_heap.top());
max_heap.pop();
}
}
double findMedian() {
if (max_heap.size() == min_heap.size()) {
return (double)(max_heap.top() + min_heap.top()) / 2;
} else {
return min_heap.top() / 1;
}
}
};
/**
* Your MedianFinder object will be instantiated and called as such:
* MedianFinder* obj = new MedianFinder();
* obj->addNum(num);
* double param_2 = obj->findMedian();
*/