-
Notifications
You must be signed in to change notification settings - Fork 125
/
Copy pathsunjae95.js
43 lines (38 loc) · 948 Bytes
/
sunjae95.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
/**
* @description
* queue의 특성을 활용하여 풀이
*
* n = length of lists
* m = length of lists[i]
* time complexity: O(n * n * m)
* space complexity: O(n*m)
*/
var mergeKLists = function (lists) {
let answer = null;
let tail = null;
let totalSize = lists.reduce((size, list) => {
let head = list;
let count = 0;
while (head) {
head = head.next;
count++;
}
return size + count;
}, 0);
while (totalSize--) {
let minIndex = lists.reduce((acc, list, i) => {
if (list === null) return acc;
if (acc === null) return { value: list.val, index: i };
return acc.value < list.val ? acc : { value: list.val, index: i };
}, null).index;
if (answer === null) {
answer = lists[minIndex];
tail = answer;
} else {
tail.next = lists[minIndex];
tail = lists[minIndex];
}
lists[minIndex] = lists[minIndex].next;
}
return answer;
};