forked from lennylxx/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
23.c
316 lines (260 loc) · 7.27 KB
/
23.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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#include <stdio.h>
#include <stdlib.h>
struct ListNode {
int val;
struct ListNode *next;
};
/*
* Suppose we have k lists, every list have n nodes.
* Use a min-heap of which size is k, heapify and take the root of heap.
* Then fill the next node of the taken node into heap, reheap.
* If we reach the end of one list, decrease heap size by 1.
* Repeat until the heap size is 0.
* Time of reheap is O(logk)
* We have k*n nodes, so T(n) = k*n*O(logk) = O(n*klogk)
*/
/* Data structures and helper function declarations. */
struct HeapNode {
struct ListNode *ptr;
int group; /* no use in this method */
};
void swap(struct HeapNode *a, struct HeapNode *b);
int comp(struct HeapNode a, struct HeapNode b);
void heapify(struct HeapNode *heap, int size); /* min heap */
void siftUp(struct HeapNode *heap, int size);
void reheap(struct HeapNode *heap, int size);
void printheap(struct HeapNode *heap, int size);
struct ListNode* mergeKLists(struct ListNode** lists, int listsSize) {
if (lists == NULL || listsSize == 0) return NULL;
struct HeapNode *heap
= (struct HeapNode *)malloc(listsSize * sizeof(struct HeapNode));
int i;
for (i = 0; i < listsSize; i++) {
heap[i].ptr = lists[i];
heap[i].group = i; /* just initialize, useless */
}
heapify(heap, listsSize);
struct ListNode *dummy = (struct ListNode *)malloc(sizeof(struct ListNode));
dummy->val = 0;
dummy->next = NULL;
struct ListNode *p = dummy;
while (listsSize && p) {
p->next = heap[0].ptr;
p = p->next;
if (heap[0].ptr && heap[0].ptr->next != NULL) {
heap[0].ptr = heap[0].ptr->next;
}
else {
swap(&heap[0], &heap[listsSize - 1]);
listsSize--;
}
reheap(heap, listsSize);
}
if (p)
p->next = NULL;
p = dummy->next;
free(dummy);
free(heap);
return p;
}
void swap(struct HeapNode *a, struct HeapNode *b) {
struct ListNode *t_ptr = a->ptr;
int t_group = a->group;
a->group = b->group;
a->ptr = b->ptr;
b->group = t_group;
b->ptr = t_ptr;
}
int comp(struct HeapNode a, struct HeapNode b) {
if (a.ptr == NULL && b.ptr == NULL) {
return 0;
}
else if (a.ptr && b.ptr == NULL) {
return -1;
}
else if (a.ptr == NULL && b.ptr) {
return 1;
}
if (a.ptr->val == b.ptr->val) {
return 0;
}
else if (a.ptr->val < b.ptr->val) {
return -1;
}
else return 1;
}
void heapify(struct HeapNode *heap, int size) {
int i;
for (i = 1; i <= size; i++) {
siftUp(heap, i);
}
}
void siftUp(struct HeapNode *heap, int size) {
int child, parent;
child = size - 1;
while (child > 0) {
parent = (child - 1) / 2;
if (comp(heap[parent], heap[child]) > 0) {
swap(&heap[parent], &heap[child]);
}
child = parent;
}
}
void reheap(struct HeapNode *heap, int size) {
int parent, child, right;
parent = 0;
child = 1;
while (child < size) {
right = child + 1;
if (right < size && comp(heap[child], heap[right]) > 0) {
child = right;
}
if (comp(heap[parent], heap[child]) <= 0) break;
swap(&heap[parent], &heap[child]);
parent = child;
child = 2 * parent + 1;
}
}
void printheap(struct HeapNode *heap, int size) {
int i;
for (i = 0; i < size; i++) {
if (heap[i].ptr) {
printf("%d ", heap[i].ptr->val);
}
else {
printf("NIL ");
}
}
printf("\n");
}
/*
* Suppose we have k lists, every list have n nodes.
* Make them to k/2 groups, merge two lists in every group.
* Then group them in k/4, then merge two lists in every group.
* Repeat until there is one group left.
* T(n) += 2n + 2n + ... + 2n = (k/2)*2n = kn
* T(n) += 4n + 4n + ... + 4n = (k/4)*4n = kn
* ...
* T(n) += 2 * (k/2)n = kn
* So, T(n) = kn * logk = O(n*klogk)
*/
struct WorkNode {
struct ListNode *ptr;
struct WorkNode *next;
};
struct ListNode* merge2Lists(struct ListNode *headA, struct ListNode *headB);
struct ListNode* mergeKLists0(struct ListNode** lists, int listsSize) {
if (lists == NULL || listsSize == 0) return NULL;
if (listsSize == 1) return lists[0];
struct WorkNode *work
= (struct WorkNode *)calloc(listsSize, sizeof(struct WorkNode));
int i;
for (i = 0; i < listsSize; i++) {
work[i].ptr = lists[i];
work[i].next = &work[i + 1];
}
work[listsSize - 1].next = NULL;
struct WorkNode *p = work;
struct WorkNode *tail = &work[listsSize - 1];
struct WorkNode *first, *second;
while (p) {
first = p;
second = p->next;
p = p->next;
if (p) p = p->next;
first->ptr = merge2Lists(first->ptr, second->ptr);
first->next = NULL;
tail->next = first;
tail = first;
}
struct ListNode *final = tail->ptr;
free(work);
return final;
}
struct ListNode* merge2Lists(struct ListNode *headA, struct ListNode *headB) {
if (headA == NULL) return headB;
if (headB == NULL) return headA;
struct ListNode *dummy = (struct ListNode *)malloc(sizeof(struct ListNode));
dummy->val = 0;
dummy->next = NULL;
struct ListNode *t = dummy;
struct ListNode *p = headA;
struct ListNode *q = headB;
while (p && q) {
if (p->val <= q->val) {
t->next = p;
p = p->next;
}
else {
t->next = q;
q = q->next;
}
t = t->next;
}
while (p) {
t->next = p;
p = p->next;
t = t->next;
}
while (q) {
t->next = q;
q = q->next;
t = t->next;
}
t = dummy->next;
free(dummy);
return t;
}
struct ListNode* generateList(int *nums, int size) {
if (nums == NULL || size == 0) return NULL;
struct ListNode *list
= (struct ListNode *)malloc(size * sizeof(struct ListNode));
struct ListNode **p = &list;
int i;
for (i = 0; i < size; i++) {
(*p)->val = nums[i];
(*p)->next = *p + 1;
p = &((*p)->next);
}
*p = NULL;
return list;
}
int main() {
int k = 7;
struct ListNode **lists
= (struct ListNode **)calloc(k, sizeof(struct ListNode*));
int n0[] = {-10,-9,-9,-3,-1,-1,0};
int n1[] = {-5};
int n2[] = {4};
int n3[] = {-8};
int n4[] = {};
int n5[] = {-9,-6,-5,-4,-2,2,3};
int n6[] = {-3,-3,-2,-1,0};
lists[0] = generateList(n0, sizeof(n0) / sizeof(int));
lists[1] = generateList(n1, sizeof(n1) / sizeof(int));
lists[2] = generateList(n2, sizeof(n2) / sizeof(int));
lists[3] = generateList(n3, sizeof(n3) / sizeof(int));
lists[4] = generateList(n4, sizeof(n4) / sizeof(int));
lists[5] = generateList(n5, sizeof(n5) / sizeof(int));
lists[6] = generateList(n6, sizeof(n6) / sizeof(int));
struct ListNode *p;
int i;
printf("Lists: \n");
for (i = 0; i < k; i++) {
p = lists[i];
while (p) {
printf("%d ", p->val);
p = p->next;
}
printf("NIL\n");
}
p = mergeKLists0(lists, k);
printf("Merged: \n");
while (p) {
printf("%d ", p->val);
p = p->next;
}
printf("NIL\n");
free(lists);
return 0;
}