-
Notifications
You must be signed in to change notification settings - Fork 4
/
merge-k-sorted-lists.js
123 lines (109 loc) · 2.57 KB
/
merge-k-sorted-lists.js
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
var DEBUG = process.env.DEBUG;
function Heap(less) {
this.less = less;
this.array = [];
this.length = 0;
}
Heap.prototype.insert = function (element) {
this.array[this.length + 1] = element;
this.length = this.length + 1;
this.fixup(Math.floor(this.length/2));
};
Heap.prototype.top = function () {
if (this.length <= 0)
throw new Error('No top element.');
return this.array[1];
};
Heap.prototype.pop = function () {
if (this.length <= 0)
throw new Error('No top element.');
var ans = this.array[1];
this.array[1] = this.array[this.length];
this.length = this.length - 1;
this.fixdown(1);
return ans;
};
Heap.prototype.fixup = function (idx) {
while (idx !== 0 && this.fix(idx) !== idx) {
idx = Math.floor(idx / 2);
}
};
Heap.prototype.fixdown = function (idx) {
var minp;
while ((minp = this.fix(idx)) !== idx) {
idx = minp;
}
};
Heap.prototype.fix = function (idx) {
var leftidx = idx*2;
var rightidx = idx*2+1;
var minp = idx;
var self = this;
[leftidx, rightidx].forEach(function (compareIdx) {
if (compareIdx <= self.length && self.less(self.array[compareIdx], self.array[minp]))
minp = compareIdx;
});
if (minp !== idx) {
var tmp = this.array[minp];
this.array[minp] = this.array[idx];
this.array[idx] = tmp;
}
return minp;
};
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode[]} lists
* @return {ListNode}
*/
var mergeKLists = function(lists) {
var head = {
val: null,
next: null
};
var curr = head;
function compare(a, b) {
if (b === null) return true;
if (a === null) return false;
return a.val < b.val;
}
var heap = new Heap(compare);
for (var i = 0; i < lists.length; i++) {
heap.insert(lists[i]);
}
while (heap.length !== 0 && heap.top() !== null) {
var min = heap.pop();
curr.next = min;
curr = curr.next;
heap.insert(min.next);
}
return head.next;
};
function test(f) {
console.log('--------Test for Heap----------');
var heap = new Heap(function (a, b) {
return a < b;
});
var heapTestSize = 100;
for (var i = 0; i < heapTestSize; i++) {
heap.insert(Math.random());
}
var prev = null;
for (var i = 0; i < heapTestSize; i++) {
var curr = heap.pop();
process.assert(prev === null || prev <= curr, 'heap is bad.');
prev = curr;
}
console.log('------Test for Heap Passed-------');
[
[[]]
].forEach(function (input) {
console.log(f.apply(undefined, input));
});
}
if (DEBUG) test(mergeKLists);