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 1368357

Browse files
committed
"Find Median from Data Stream": alt to c solution
1 parent 99d38cc commit 1368357

File tree

3 files changed

+178
-143
lines changed

3 files changed

+178
-143
lines changed

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ The `☢` means that you need to have a LeetCode Premium Subscription.
2626
| | Problem | Solution |
2727
| --- | ------------------------------------------------------------ | ------------------ |
2828
| 296 | [Best Meeting Point]| |
29-
| 295 | [Find Median from Data Stream] | [C++](src/295.cpp) |
29+
| 295 | [Find Median from Data Stream] | [C](src/295.c) |
3030
| 294 | [Flip Game II]| |
3131
| 293 | [Flip Game]| |
3232
| 292 | [Nim Game] | [C](src/292.c) |

‎src/295.c

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
static const int MAXN = 102400;
5+
6+
struct MedianFinder {
7+
int *maxHeap; /* store the smaller half */
8+
int *minHeap; /* store the larger half */
9+
int maxHeapSize;
10+
int minHeapSize;
11+
double medium;
12+
};
13+
14+
void swap(int *a, int *b) {
15+
int c = *a;
16+
*a = *b;
17+
*b = c;
18+
}
19+
20+
void addheap(int *heap, int *size, int data) {
21+
if (*size < MAXN - 1) {
22+
heap[*size] = data;
23+
(*size)++;
24+
}
25+
}
26+
27+
void siftUpMax(int *heap, int size) {
28+
int parent, child;
29+
child = size - 1;
30+
while (child > 0) {
31+
parent = (child - 1) / 2;
32+
if (heap[parent] < heap[child])
33+
swap(&heap[parent], &heap[child]);
34+
child = parent;
35+
}
36+
}
37+
38+
void siftDownMax(int *heap, int size) {
39+
int parent, child;
40+
parent = 0; child = 1;
41+
while (child < size) {
42+
if (child + 1 < size && heap[child] < heap[child + 1])
43+
child = child + 1;
44+
45+
if (heap[parent] >= heap[child]) break;
46+
swap(&heap[parent], &heap[child]);
47+
parent = child;
48+
child = parent * 2 + 1;
49+
}
50+
}
51+
52+
void siftUpMin(int *heap, int size) {
53+
int parent, child;
54+
child = size - 1;
55+
while (child > 0) {
56+
parent = (child - 1) / 2;
57+
if (heap[parent] > heap[child])
58+
swap(&heap[parent], &heap[child]);
59+
child = parent;
60+
}
61+
}
62+
63+
void siftDownMin(int *heap, int size) {
64+
int parent, child;
65+
parent = 0; child = 1;
66+
while (child < size) {
67+
if (child + 1 < size && heap[child] > heap[child + 1])
68+
child = child + 1;
69+
70+
if (heap[parent] <= heap[child]) break;
71+
swap(&heap[parent], &heap[child]);
72+
parent = child;
73+
child = parent * 2 + 1;
74+
}
75+
}
76+
77+
/** Initialize your data structure here. */
78+
struct MedianFinder* MedianFinderCreate() {
79+
struct MedianFinder *mf = (struct MedianFinder *)malloc(sizeof(struct MedianFinder));
80+
mf->maxHeap = (int *)malloc(MAXN * sizeof(int));
81+
mf->minHeap = (int *)malloc(MAXN * sizeof(int));
82+
mf->minHeapSize = mf->maxHeapSize = 0;
83+
mf->medium = 0;
84+
return mf;
85+
}
86+
87+
/** Inserts a num into the data structure. */
88+
void addNum(struct MedianFinder* mf, int num) {
89+
if (mf == NULL) return;
90+
91+
if (mf->maxHeapSize == mf->minHeapSize) {
92+
if (num > mf->medium) {
93+
addheap(mf->minHeap, &mf->minHeapSize, num);
94+
siftUpMin(mf->minHeap, mf->minHeapSize);
95+
mf->medium = mf->minHeap[0];
96+
}
97+
else {
98+
addheap(mf->maxHeap, &mf->maxHeapSize, num);
99+
siftUpMax(mf->maxHeap, mf->maxHeapSize);
100+
mf->medium = mf->maxHeap[0];
101+
}
102+
}
103+
else {
104+
if (num > mf->minHeap[0]) {
105+
addheap(mf->minHeap, &mf->minHeapSize, num);
106+
siftUpMin(mf->minHeap, mf->minHeapSize);
107+
if (mf->minHeapSize >= mf->maxHeapSize + 2) {
108+
addheap(mf->maxHeap, &mf->maxHeapSize, mf->minHeap[0]);
109+
siftUpMax(mf->maxHeap, mf->maxHeapSize);
110+
swap(&mf->minHeap[0], &mf->minHeap[mf->minHeapSize - 1]);
111+
mf->minHeapSize--;
112+
siftDownMin(mf->minHeap, mf->minHeapSize);
113+
}
114+
}
115+
else {
116+
addheap(mf->maxHeap, &mf->maxHeapSize, num);
117+
siftUpMax(mf->maxHeap, mf->maxHeapSize);
118+
if (mf->maxHeapSize >= mf->minHeapSize + 2) {
119+
addheap(mf->minHeap, &mf->minHeapSize, mf->maxHeap[0]);
120+
siftUpMin(mf->minHeap, mf->minHeapSize);
121+
swap(&mf->maxHeap[0], &mf->maxHeap[mf->maxHeapSize - 1]);
122+
mf->maxHeapSize--;
123+
siftDownMax(mf->maxHeap, mf->maxHeapSize);
124+
}
125+
}
126+
mf->medium = mf->maxHeap[0] + (mf->minHeap[0] - mf->maxHeap[0]) / 2.0;
127+
}
128+
}
129+
130+
/** find the median of current data stream */
131+
double findMedian(struct MedianFinder* mf) {
132+
if (mf == NULL) return 0;
133+
return mf->medium;
134+
}
135+
136+
/** Deallocates memory previously allocated for the data structure. */
137+
void MedianFinderFree(struct MedianFinder* mf) {
138+
if (mf == NULL) return;
139+
free(mf->maxHeap);
140+
free(mf->minHeap);
141+
free(mf);
142+
}
143+
144+
// Your MedianFinder object will be instantiated and called as such:
145+
// struct MedianFinder* mf = MedianFinderCreate();
146+
// addNum(mf, 1.0);
147+
// findMedian(mf);
148+
// MedianFinderFree(mf);
149+
150+
int main() {
151+
struct MedianFinder* mf = MedianFinderCreate();
152+
153+
addNum(mf, 1);
154+
printf("%lf\n", findMedian(mf));
155+
addNum(mf, 2);
156+
printf("%lf\n", findMedian(mf));
157+
addNum(mf, 3);
158+
printf("%lf\n", findMedian(mf));
159+
addNum(mf, 4);
160+
printf("%lf\n", findMedian(mf));
161+
addNum(mf, 5);
162+
printf("%lf\n", findMedian(mf));
163+
addNum(mf, 6);
164+
printf("%lf\n", findMedian(mf));
165+
addNum(mf, 7);
166+
printf("%lf\n", findMedian(mf));
167+
addNum(mf, 8);
168+
printf("%lf\n", findMedian(mf));
169+
addNum(mf, 9);
170+
printf("%lf\n", findMedian(mf));
171+
addNum(mf, 10);
172+
printf("%lf\n", findMedian(mf));
173+
174+
MedianFinderFree(mf);
175+
176+
return 0;
177+
}

‎src/295.cpp

Lines changed: 0 additions & 142 deletions
This file was deleted.

0 commit comments

Comments
(0)

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