Skip to content

Commit

Permalink
5. Merge k Sorted Lists
Browse files Browse the repository at this point in the history
  • Loading branch information
Sunjae95 committed Oct 16, 2024
1 parent f4b3dc4 commit cfa4c54
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions merge-k-sorted-lists/sunjae95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
};

0 comments on commit cfa4c54

Please sign in to comment.