-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathcontiunous-median.cpp
49 lines (43 loc) · 1.21 KB
/
contiunous-median.cpp
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
#include <queue>
using namespace std;
// Do not edit the class below except for
// the insert method. Feel free to add new
// properties and methods to the class.
class ContinuousMedianHandler {
public:
double median;
priority_queue<int, vector<int>> max_heap;
priority_queue<int, vector<int>, greater<int>> min_heap;
void insert(int number) {
// Write your code here.
if (empty(max_heap)) {
max_heap.emplace(number);
median = number;
return;
} else {
if (number < max_heap.top()) {
max_heap.emplace(number);
} else {
min_heap.emplace(number);
}
if (min_heap.size() - max_heap.size() == 2) {
max_heap.emplace(min_heap.top());
min_heap.pop();
} else if (max_heap.size() - min_heap.size() == 2) {
min_heap.emplace(max_heap.top());
max_heap.pop();
}
if (min_heap.size() == max_heap.size()) {
median = (double) (min_heap.top() + max_heap.top()) / 2.0;
} else if (min_heap.size() < max_heap.size()) {
median = (double) max_heap.top();
} else {
median = (double) min_heap.top();
}
return;
}
}
double getMedian() {
return median;
}
};