-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_295_FindMedianFromDataStream.cpp
73 lines (60 loc) · 1.81 KB
/
_295_FindMedianFromDataStream.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* Source - https://leetcode.com/problems/find-median-from-data-stream/
Author - Shivam Arora
*/
#include <bits/stdc++.h>
using namespace std;
class MedianFinder {
priority_queue<double> left;
priority_queue<double, vector<double>, greater<double>> right;
public:
MedianFinder() {}
void addNum(int num) {
if(right.size() > 0 && num > right.top())
right.push(num);
else
left.push(num);
if(left.size() - right.size() == 2) {
int l = left.top();
left.pop();
right.push(l);
} else if(right.size() - left.size() == 2) {
int r = right.top();
right.pop();
left.push(r);
}
}
double findMedian() {
if(left.size() == right.size())
return (left.top() + right.top()) / 2;
else if(left.size() > right.size())
return left.top();
else
return right.top();
}
};
int main()
{
int n;
cout<<"Enter size of input: ";
cin>>n;
vector<string> operations(n);
cout<<"Enter operations (for addNum, write 'addNum <value>'): "<<endl;
for(int i = 0; i < n; i++)
getline(cin>>ws, operations[i]);
MedianFinder obj;
vector<string> result;
for(int i = 0; i < n; i++) {
if(operations[i].find("addNum") != string::npos) {
string token = operations[i].substr(operations[i].find(" ") + 1);
obj.addNum(stoi(token));
result.push_back("null");
} else if(operations[i].find("findMedian") != string::npos) {
result.push_back(to_string(obj.findMedian()));
} else
result.push_back("null");
}
cout<<"Output: ";
for(int i = 0; i < n; i++)
cout<<result[i]<<" ";
cout<<endl;
}