Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 99d38cc

Browse files
committed
"Find Median from Data Stream"
1 parent 86ff32f commit 99d38cc

File tree

2 files changed

+150
-0
lines changed

2 files changed

+150
-0
lines changed

‎README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ The `☢` means that you need to have a LeetCode Premium Subscription.
2525

2626
| | Problem | Solution |
2727
| --- | ------------------------------------------------------------ | ------------------ |
28+
| 296 | [Best Meeting Point]| |
29+
| 295 | [Find Median from Data Stream] | [C++](src/295.cpp) |
30+
| 294 | [Flip Game II]| |
31+
| 293 | [Flip Game]| |
2832
| 292 | [Nim Game] | [C](src/292.c) |
2933
| 291 | [Word Pattern II]| |
3034
| 290 | [Word Pattern] | [C++](src/290.cpp) |
@@ -305,6 +309,10 @@ The `☢` means that you need to have a LeetCode Premium Subscription.
305309
[LeetCode algorithm problems]: https://leetcode.com/problemset/algorithms/
306310

307311

312+
[Best Meeting Point]: https://leetcode.com/problems/best-meeting-point/
313+
[Find Median from Data Stream]: https://leetcode.com/problems/find-median-from-data-stream/
314+
[Flip Game II]: https://leetcode.com/problems/flip-game-ii/
315+
[Flip Game]: https://leetcode.com/problems/flip-game/
308316
[Nim Game]: https://leetcode.com/problems/nim-game/
309317
[Word Pattern II]: https://leetcode.com/problems/word-pattern-ii/
310318
[Word Pattern]: https://leetcode.com/problems/word-pattern/

‎src/295.cpp

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
class MedianFinder {
6+
static const int MAXN = 102400;
7+
int maxHeap[MAXN]; // store the smaller half
8+
int minHeap[MAXN]; // store the bigger half
9+
int max_heap_size = 0;
10+
int min_heap_size = 0;
11+
double medium = 0;
12+
13+
void addheap(int *heap, int &size, int data) {
14+
if (size < MAXN - 1) {
15+
heap[size] = data;
16+
size++;
17+
}
18+
}
19+
20+
void siftUpMax() {
21+
int parent, child;
22+
child = max_heap_size - 1;
23+
while (child > 0) {
24+
parent = (child - 1) / 2;
25+
if (maxHeap[parent] < maxHeap[child])
26+
swap(maxHeap[parent], maxHeap[child]);
27+
child = parent;
28+
}
29+
}
30+
31+
void siftDownMax() {
32+
int parent, child;
33+
parent = 0; child = 1;
34+
while (child < max_heap_size) {
35+
if (child + 1 < max_heap_size && maxHeap[child] < maxHeap[child + 1])
36+
child = child + 1;
37+
38+
if (maxHeap[parent] >= maxHeap[child]) break;
39+
swap(maxHeap[parent], maxHeap[child]);
40+
parent = child;
41+
child = parent * 2 + 1;
42+
}
43+
}
44+
45+
void siftUpMin() {
46+
int parent, child;
47+
child = min_heap_size - 1;
48+
while (child > 0) {
49+
parent = (child - 1) / 2;
50+
if (minHeap[parent] > minHeap[child])
51+
swap(minHeap[parent], minHeap[child]);
52+
child = parent;
53+
}
54+
}
55+
56+
void siftDownMin() {
57+
int parent, child;
58+
parent = 0; child = 1;
59+
while (child < min_heap_size) {
60+
if (child + 1< min_heap_size && minHeap[child] > minHeap[child + 1])
61+
child = child + 1;
62+
63+
if (minHeap[parent] <= minHeap[child]) break;
64+
swap(minHeap[parent], minHeap[child]);
65+
parent = child;
66+
child = parent * 2 + 1;
67+
}
68+
}
69+
70+
public:
71+
// Adds a number into the data structure.
72+
void addNum(int num) {
73+
if (max_heap_size == min_heap_size) {
74+
if (num > medium) {
75+
addheap(minHeap, min_heap_size, num);
76+
siftUpMin();
77+
medium = minHeap[0];
78+
}
79+
else {
80+
addheap(maxHeap, max_heap_size, num);
81+
siftUpMax();
82+
medium = maxHeap[0];
83+
}
84+
}
85+
else {
86+
if (num > minHeap[0]) {
87+
addheap(minHeap, min_heap_size, num);
88+
siftUpMin();
89+
if (min_heap_size >= max_heap_size + 2) {
90+
addheap(maxHeap, max_heap_size, minHeap[0]);
91+
siftUpMax();
92+
swap(minHeap[0], minHeap[min_heap_size - 1]);
93+
min_heap_size--;
94+
siftDownMin();
95+
}
96+
}
97+
else {
98+
addheap(maxHeap, max_heap_size, num);
99+
siftUpMax();
100+
if (max_heap_size >= min_heap_size + 2) {
101+
addheap(minHeap, min_heap_size, maxHeap[0]);
102+
siftUpMin();
103+
swap(maxHeap[0], maxHeap[max_heap_size - 1]);
104+
max_heap_size--;
105+
siftDownMax();
106+
}
107+
}
108+
medium = maxHeap[0] + (minHeap[0] - maxHeap[0]) / 2.0;
109+
}
110+
}
111+
112+
// Returns the median of current data stream
113+
double findMedian() {
114+
return medium;
115+
}
116+
};
117+
118+
int main() {
119+
MedianFinder mf;
120+
mf.addNum(1);
121+
cout << mf.findMedian() << endl;
122+
mf.addNum(2);
123+
cout << mf.findMedian() << endl;
124+
mf.addNum(3);
125+
cout << mf.findMedian() << endl;
126+
mf.addNum(4);
127+
cout << mf.findMedian() << endl;
128+
mf.addNum(5);
129+
cout << mf.findMedian() << endl;
130+
mf.addNum(6);
131+
cout << mf.findMedian() << endl;
132+
mf.addNum(7);
133+
cout << mf.findMedian() << endl;
134+
mf.addNum(8);
135+
cout << mf.findMedian() << endl;
136+
mf.addNum(9);
137+
cout << mf.findMedian() << endl;
138+
mf.addNum(10);
139+
cout << mf.findMedian() << endl;
140+
141+
return 0;
142+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /