-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathfind_median_from_data_stream_fenwick_tree_solution.cpp
More file actions
84 lines (64 loc) · 1.62 KB
/
find_median_from_data_stream_fenwick_tree_solution.cpp
File metadata and controls
84 lines (64 loc) · 1.62 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
/*
* Find Median from Data Stream
* Link: https://leetcode.com/problems/find-median-from-data-stream/
*/
class fenwick_tree {
private:
long max_value;
unordered_map<long, int> f;
inline int sum(long pos) {
int res = 0;
while (pos >= 0) {
res += f[pos];
pos = (pos & (pos + 1)) - 1;
}
return res;
}
public:
inline fenwick_tree() : max_value(INT_MAX - 1) {}
inline void add(long pos, int val) {
pos += max_value >> 1;
while (pos <= max_value) {
f[pos] += val;
pos |= pos + 1;
}
}
inline long find_by_order(int ord) {
long low = 0, high = max_value;
while (low <= high) {
long mid = (low + high) >> 1;
if (sum(mid) > ord) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return high + 1;
}
};
class MedianFinder {
private:
int size;
fenwick_tree f;
public:
/** initialize your data structure here. */
MedianFinder() : size(0) {}
void addNum(int num) {
f.add(num, 1);
size++;
}
double findMedian() {
if (size == 0) {
return 0.0;
}
double first = (double) f.find_by_order(size >> 1);
double second = size & 1 ? first : (double) f.find_by_order((size - 1) >> 1);
return 0.5 * (first + second - INT_MAX + 1);
}
};
/**
* Your MedianFinder object will be instantiated and called as such:
* MedianFinder *obj = new MedianFinder();
* obj->addNum(num);
* double param_2 = obj->findMedian();
*/