-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy path295. Find Median from Data Stream.cpp
239 lines (207 loc) · 6.38 KB
/
295. Find Median from Data Stream.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
//Approach 1: Simple Sorting
//TLE
//15 / 18 test cases passed.
//time: O(NlogN), space: O(N)
class MedianFinder {
public:
vector<int> vec;
/** initialize your data structure here. */
MedianFinder() {
}
void addNum(int num) {
vec.push_back(num);
sort(vec.begin(), vec.end());
}
double findMedian() {
int n = vec.size();
if(n&1) return vec[n>>1];
return (vec[(n>>1)-1]+vec[n>>1])/2.0;
}
};
/**
* Your MedianFinder object will be instantiated and called as such:
* MedianFinder* obj = new MedianFinder();
* obj->addNum(num);
* double param_2 = obj->findMedian();
*/
//Approach 2: Insertion Sort
//Runtime: 444 ms, faster than 15.81% of C++ online submissions for Find Median from Data Stream.
//Memory Usage: 46.8 MB, less than 94.97% of C++ online submissions for Find Median from Data Stream.
//time: O(N+logN) = O(N), space: O(N)
class MedianFinder {
public:
vector<int> vec;
/** initialize your data structure here. */
MedianFinder() {
}
void addNum(int num) {
if(vec.empty()){
vec.push_back(num);
}else{
/*
lower_bound: find the smallest number >= x
if there is no such element, it returns vec.end(),
in this case, this statement is still safe
*/
vec.insert(lower_bound(vec.begin(), vec.end(), num), num);
}
}
double findMedian() {
int n = vec.size();
if(n&1) return vec[n>>1];
return (vec[(n>>1)-1]+vec[n>>1])/2.0;
}
};
//Approach 3: Two Heaps
//Runtime: 424 ms, faster than 18.81% of C++ online submissions for Find Median from Data Stream.
//Memory Usage: 47 MB, less than 41.77% of C++ online submissions for Find Median from Data Stream.
//time: O(logN), space: O(N)
class MedianFinder {
public:
//maxPQ: smaller half, may contain one more element than minPQ
//minPQ: larger half
priority_queue<int, vector<int>> maxPQ;
priority_queue<int, vector<int>, greater<int>> minPQ;
/** initialize your data structure here. */
MedianFinder() {
}
void addNum(int num) {
maxPQ.push(num);
int t = maxPQ.top(); maxPQ.pop();
minPQ.push(t);
//minPQ's size must be <= maxPQ's size
if(minPQ.size() > maxPQ.size()){
t = minPQ.top(); minPQ.pop();
maxPQ.push(t);
}
}
double findMedian() {
if(maxPQ.size() > minPQ.size()){
return maxPQ.top();
}
return (maxPQ.top()+minPQ.top())/2.0;
}
};
//Approach 4: Multiset and Two Pointers
//Runtime: 220 ms, faster than 95.39% of C++ online submissions for Find Median from Data Stream.
//Memory Usage: 49.3 MB, less than 9.29% of C++ online submissions for Find Median from Data Stream.
//time: O(logN), space: O(N)
class MedianFinder {
public:
//AVL tree
multiset<int> mset;
multiset<int>::iterator lo, hi;
/** initialize your data structure here. */
MedianFinder() {
}
void addNum(int num) {
int n = mset.size();
multiset<int>::iterator it = mset.insert(num);
if(!n){
//originally empty
lo = hi = it;
}else if(n&1){
//odd number of elements, lo and hi points to same location
if(num < *lo){
/*
[1,2,3]
becomes
[1,1,2,3]
*/
--lo;
}else if(num == *lo){
/*
In C++, if there are already elements equal to num,
then it insert num after such elements,
so larger half's size will increase
[1,2,3]: lo and hi points to 2
becomes
[1,2,2,3]: lo points to 1st 2, hi points to 2nd 2
*/
++hi;
}else{
//num > *lo
++hi;
}
}else{
//even number of elements
if(*lo <= num && num < *hi){
//note the <= and < here!!
/*
In C++, if there are already elements equal to num,
then it insert num after such elements
so when num equal to *lo,
it will also be inserted btw the two pointers
*/
lo = hi = it;
}else if(num < *lo){
/*
only when num < *lo(not ==),
num is inserted before lo
[1,3,4,5]
becomes
[1,2,3,4,5]
*/
hi = lo;
//lo and hi both points to old lo
}else{
/*
when num >= *hi,
num is always inserted after hi
[1,3,4,5]
becomes
[1,3,4,5,6]
*/
lo = hi;
//lo and hi both points to old hi
}
}
}
double findMedian() {
return (*lo + *hi)/2.0;
}
};
//Multiset and One Pointer
//Runtime: 300 ms, faster than 44.92% of C++ online submissions for Find Median from Data Stream.
//Memory Usage: 49.5 MB, less than 5.62% of C++ online submissions for Find Median from Data Stream.
class MedianFinder {
public:
multiset<int> mset;
//mid points to the lower median if mset have even elements
multiset<int>::iterator mid;
/** initialize your data structure here. */
MedianFinder() {
}
void addNum(int num) {
int n = mset.size();
mset.insert(num);
if(n == 0){
mid = mset.begin();
}else if(num < *mid){
/*
[1,3,4]
becomes
[1,2,3,4]
[1,3,4,5]
beocmes
[1,2,3,4,5]
*/
mid = (n&1) ? prev(mid) : mid;
}else{
//num >= *mid, num is inserted after mid
/*
[1,3,4]
becomes
[1,3,4,6]
[1,3,4,5]
beocmes
[1,3,4,5,6]
*/
mid = (n&1) ? mid : next(mid);
}
}
double findMedian() {
int n = mset.size();
return (n&1) ? *mid: (*mid + *(next(mid)))/2.0;
}
};