-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path295.find-median-from-data-stream.cpp
More file actions
53 lines (48 loc) · 1.34 KB
/
295.find-median-from-data-stream.cpp
File metadata and controls
53 lines (48 loc) · 1.34 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
47
48
49
50
51
52
53
class MedianFinder {
public:
/** initialize your data structure here. */
std::priority_queue<int> lower_half;
std::priority_queue<int, std::vector<int>, std::greater<int>> upper_half;
MedianFinder() : lower_half(), upper_half(std::greater<int>()) {}
void addNum(int num) {
if (!(lower_half.size() + upper_half.size())) {
upper_half.push(num);
return;
}
if (num < upper_half.top()) {
lower_half.push(num);
} else {
upper_half.push(num);
}
rebalance();
}
void rebalance() {
if (std::fabs(static_cast<int>(lower_half.size()) -
static_cast<int>(upper_half.size())) > 1) {
if (lower_half.size() > upper_half.size()) {
int top = lower_half.top();
lower_half.pop();
upper_half.push(top);
} else {
int top = upper_half.top();
upper_half.pop();
lower_half.push(top);
}
}
}
double findMedian() {
if (!((lower_half.size() + upper_half.size()) % 2)) {
return (lower_half.top() + upper_half.top()) / 2.0;
}
if (upper_half.size() > lower_half.size()) {
return upper_half.top();
}
return lower_half.top();
}
};
/**
* Your MedianFinder object will be instantiated and called as such:
* MedianFinder* obj = new MedianFinder();
* obj->addNum(num);
* double param_2 = obj->findMedian();
*/