-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedianFinder.h
54 lines (50 loc) · 1.45 KB
/
medianFinder.h
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
//
// Created by so_go on 2019/5/29.
//
#ifndef SRC_MEDIANFINDER_H
#define SRC_MEDIANFINDER_H
#include<vector>
#include<queue>
#include<functional>
using namespace std;
class MedianFinder {
private:
/** initialize your data structure here. */
priority_queue<int, vector<int>, less<int>> max_heap; // 大顶堆
priority_queue<int, vector<int>, greater<int>> min_heap; // 小顶堆
public:
void addNum(int num) {
int tmp;
if( max_heap.size() == 0 && min_heap.size() == 0){
max_heap.push(num);
}
else {
if(num < max_heap.top()){
max_heap.push(num);
if( max_heap.size() - min_heap.size() > 1){
tmp = max_heap.top();
max_heap.pop();
min_heap.push(tmp);
}
}
else{
min_heap.push(num);
if( min_heap.size() - max_heap.size() > 1){
tmp = min_heap.top();
min_heap.pop();
max_heap.push(tmp);
}
}
}
// cout << max_heap.size() << ' ' << min_heap.size() << endl;
}
double findMedian() {
if(min_heap.size() == max_heap.size()){
return (min_heap.top() + max_heap.top())/2.0;
}
else{
return min_heap.size() > max_heap.size() ? min_heap.top() : max_heap.top();
}
}
};
#endif //SRC_MEDIANFINDER_H