-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path295.cpp
More file actions
92 lines (83 loc) · 2.27 KB
/
295.cpp
File metadata and controls
92 lines (83 loc) · 2.27 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
class MedianFinder {
public:
/** initialize your data structure here. */
priority_queue<int> max_heap;
priority_queue<int, vector<int>, greater<int>> min_heap;
MedianFinder() {}
void addNum(int num) {
if(!max_heap.size()){
max_heap.push(num);
return;
}
if(max_heap.size()>min_heap.size()){
if(num>=max_heap.top()){
min_heap.push(num);
return;
}
min_heap.push(max_heap.top());
max_heap.pop();
max_heap.push(num);
return;
}
else{
if(min_heap.top()>=num){
max_heap.push(num);
return;
}
max_heap.push(min_heap.top());
min_heap.pop();
min_heap.push(num);
return;
}
}
double findMedian() {
if(max_heap.size()==min_heap.size()){
return (max_heap.top() + min_heap.top())/2.0;
}
else{
return max_heap.top();
}
}
};
/**
* Your MedianFinder object will be instantiated and called as such:
* MedianFinder* obj = new MedianFinder();
* obj->addNum(num);
* double param_2 = obj->findMedian();
*/
// Best Sol
class MedianFinder {
public:
/** initialize your data structure here. */
priority_queue<int> left;
priority_queue<int,vector<int>,greater<int>> right;
MedianFinder() {ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr);}
void addNum(int num) {
if(left.size()==0){
left.push(num);
return;
}
if(num<=left.top())
left.push(num);
else
right.push(num);
if((left.size()-right.size()) == 2){
right.push(left.top());
left.pop();
}
if((right.size()-left.size()) == 2){
left.push(right.top());
right.pop();
}
}
double findMedian() {
double ans;
if(left.size()>right.size())
ans=(double) left.top();
else if(right.size()>left.size())
ans=(double) right.top();
else
ans=(double) (left.top()+right.top())/2;
return ans;
}
};