forked from lennylxx/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
295.c
188 lines (169 loc) · 5.43 KB
/
295.c
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
#include <stdio.h>
#include <stdlib.h>
struct MedianFinder {
int *maxHeap; /* store the smaller half */
int *minHeap; /* store the larger half */
int maxHeapCapacity;
int minHeapCapacity;
int maxHeapSize;
int minHeapSize;
double median;
};
void swap(int *a, int *b) {
int c = *a;
*a = *b;
*b = c;
}
void addHeap(int **heap, int *size, int *capacity, int data) {
if (*size == *capacity) {
int newCapacity = (*capacity) * 2; /* resize */
int *newHeap = (int *)malloc(newCapacity * sizeof(int));
for (int i = 0; i < *capacity; i++) {
newHeap[i] = (*heap)[i];
}
if (*heap)
free(*heap);
*heap = newHeap;
*capacity = newCapacity;
}
(*heap)[*size] = data;
(*size)++;
}
void siftUpMax(int *heap, int size) {
int parent, child;
child = size - 1;
while (child > 0) {
parent = (child - 1) / 2;
if (heap[parent] < heap[child])
swap(&heap[parent], &heap[child]);
child = parent;
}
}
void siftDownMax(int *heap, int size) {
int parent, child;
parent = 0; child = 1;
while (child < size) {
if (child + 1 < size && heap[child] < heap[child + 1])
child = child + 1;
if (heap[parent] >= heap[child]) break;
swap(&heap[parent], &heap[child]);
parent = child;
child = parent * 2 + 1;
}
}
void siftUpMin(int *heap, int size) {
int parent, child;
child = size - 1;
while (child > 0) {
parent = (child - 1) / 2;
if (heap[parent] > heap[child])
swap(&heap[parent], &heap[child]);
child = parent;
}
}
void siftDownMin(int *heap, int size) {
int parent, child;
parent = 0; child = 1;
while (child < size) {
if (child + 1 < size && heap[child] > heap[child + 1])
child = child + 1;
if (heap[parent] <= heap[child]) break;
swap(&heap[parent], &heap[child]);
parent = child;
child = parent * 2 + 1;
}
}
/** Initialize your data structure here. */
struct MedianFinder* MedianFinderCreate() {
struct MedianFinder *mf = (struct MedianFinder *)malloc(sizeof(struct MedianFinder));
mf->maxHeap = (int *)malloc(sizeof(int));
mf->minHeap = (int *)malloc(sizeof(int));
mf->maxHeap[0] = mf->minHeap[0] = 0;
mf->minHeapSize = mf->maxHeapSize = 0;
mf->minHeapCapacity = mf->maxHeapCapacity = 1;
mf->median = 0;
return mf;
}
/** Inserts a num into the data structure. */
void addNum(struct MedianFinder* mf, int num) {
if (mf == NULL) return;
if (mf->maxHeapSize == mf->minHeapSize) {
if (num > mf->median) {
addHeap(&mf->minHeap, &mf->minHeapSize, &mf->minHeapCapacity, num);
siftUpMin(mf->minHeap, mf->minHeapSize);
mf->median = mf->minHeap[0];
}
else {
addHeap(&mf->maxHeap, &mf->maxHeapSize, &mf->maxHeapCapacity, num);
siftUpMax(mf->maxHeap, mf->maxHeapSize);
mf->median = mf->maxHeap[0];
}
}
else {
if (num > mf->minHeap[0]) {
addHeap(&mf->minHeap, &mf->minHeapSize, &mf->minHeapCapacity, num);
siftUpMin(mf->minHeap, mf->minHeapSize);
if (mf->minHeapSize >= mf->maxHeapSize + 2) {
addHeap(&mf->maxHeap, &mf->maxHeapSize, &mf->maxHeapCapacity, mf->minHeap[0]);
siftUpMax(mf->maxHeap, mf->maxHeapSize);
swap(&mf->minHeap[0], &mf->minHeap[mf->minHeapSize - 1]);
mf->minHeapSize--;
siftDownMin(mf->minHeap, mf->minHeapSize);
}
}
else {
addHeap(&mf->maxHeap, &mf->maxHeapSize, &mf->maxHeapCapacity, num);
siftUpMax(mf->maxHeap, mf->maxHeapSize);
if (mf->maxHeapSize >= mf->minHeapSize + 2) {
addHeap(&mf->minHeap, &mf->minHeapSize, &mf->minHeapCapacity, mf->maxHeap[0]);
siftUpMin(mf->minHeap, mf->minHeapSize);
swap(&mf->maxHeap[0], &mf->maxHeap[mf->maxHeapSize - 1]);
mf->maxHeapSize--;
siftDownMax(mf->maxHeap, mf->maxHeapSize);
}
}
mf->median = mf->maxHeap[0] + (mf->minHeap[0] - mf->maxHeap[0]) / 2.0;
}
}
/** find the median of current data stream */
double findMedian(struct MedianFinder* mf) {
if (mf == NULL) return 0;
return mf->median;
}
/** Deallocates memory previously allocated for the data structure. */
void MedianFinderFree(struct MedianFinder* mf) {
if (mf == NULL) return;
if (mf->maxHeap) free(mf->maxHeap);
if (mf->minHeap) free(mf->minHeap);
free(mf);
}
// Your MedianFinder object will be instantiated and called as such:
// struct MedianFinder* mf = MedianFinderCreate();
// addNum(mf, 1.0);
// findMedian(mf);
// MedianFinderFree(mf);
int main() {
struct MedianFinder* mf = MedianFinderCreate();
addNum(mf, 1);
printf("%lf\n", findMedian(mf));
addNum(mf, 2);
printf("%lf\n", findMedian(mf));
addNum(mf, 3);
printf("%lf\n", findMedian(mf));
addNum(mf, 4);
printf("%lf\n", findMedian(mf));
addNum(mf, 5);
printf("%lf\n", findMedian(mf));
addNum(mf, 6);
printf("%lf\n", findMedian(mf));
addNum(mf, 7);
printf("%lf\n", findMedian(mf));
addNum(mf, 8);
printf("%lf\n", findMedian(mf));
addNum(mf, 9);
printf("%lf\n", findMedian(mf));
addNum(mf, 10);
printf("%lf\n", findMedian(mf));
MedianFinderFree(mf);
return 0;
}