-
Notifications
You must be signed in to change notification settings - Fork 41
/
solution.ts
80 lines (74 loc) · 1.88 KB
/
solution.ts
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
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/
function mergeKLists (lists: Array<ListNode | null>): ListNode | null {
const heap:ListNode[] = [];
for (let i = 0; i < lists.length; i++) {
if (!lists[i]) {
continue;
}
add2heap(heap, lists[i]);
}
const dummyHead = new ListNode(0);
let tail = dummyHead;
while (heap.length) {
tail.next = removeTop(heap);
tail = tail.next;
}
return dummyHead.next;
}
function add2heap (heap:ListNode[], head:ListNode) {
heap.push(head);
let index = heap.length - 1;
while (index > 0) {
const parent = ((index + 1) >> 1) - 1;
if (heap[parent].val > heap[index].val) {
swap(heap, index, parent);
index = parent;
} else {
break;
}
}
}
function swap<T> (arr:T[], i:number, j:number) {
const tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
function sink (heap:ListNode[]) {
let index = 0;
while (2 * index + 1 < heap.length) {
let child = 2 * index + 1;
if (child + 1 < heap.length && heap[child + 1].val < heap[child].val) {
child++;
}
if (heap[child].val < heap[index].val) {
swap(heap, index, child);
index = child;
} else {
break;
}
}
}
function removeTop (heap:ListNode[]):ListNode {
const top = heap[0];
const next = top.next;
top.next = null;
if (next) {
heap[0] = next;
sink(heap);
} else {
heap[0] = heap[heap.length - 1];
heap.pop();
heap.length && sink(heap);
}
return top;
}