Skip to content

Commit 203ebfd

Browse files
committed
Fix merge-k-sorted-lists solution
1 parent 62212e2 commit 203ebfd

File tree

1 file changed

+0
-82
lines changed

1 file changed

+0
-82
lines changed

β€Žmerge-k-sorted-lists/Jeehay28.jsβ€Ž

Lines changed: 0 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,3 @@
1-
// Priority Queue (Min-Heap) - Best for larger cases
2-
// πŸ•’ Time Complexity: O(N log K), where N is the total number of nodes, K is the number of lists
3-
// πŸ—‚οΈ Space Complexity: O(K)
4-
5-
//
6-
/** const PriorityQueue = require('datastructures-js').PriorityQueue;
7-
* // This will allow you to use the heap-based approach with a priority queue, which should be more efficient than the brute-force sorting method, especially for larger input sizes.
8-
/**
9-
* Definition for singly-linked list.
10-
* function ListNode(val, next) {
11-
* this.val = val === undefined ? 0 : val;
12-
* this.next = next === undefined ? null : next;
13-
* }
14-
*/
15-
/**
16-
* @param {ListNode[]} lists
17-
* @return {ListNode}
18-
*/
19-
20-
var maxDepth = function (root) {
21-
// Base case: if the node is null, the depth is 0
22-
if (root === null) return 0;
23-
24-
// Recursively calculate the depth of the left and right subtrees
25-
let leftDepth = maxDepth(root.left);
26-
let rightDepth = maxDepth(root.right);
27-
28-
// Return the maximum of the two depths plus 1 for the current node
29-
return Math.max(leftDepth, rightDepth) + 1;
30-
};
31-
32-
var mergeKLists = function (lists) {
33-
// Create a priority queue (min-heap) to store the nodes
34-
const pq = new PriorityQueue((a, b) => a.val - b.val);
35-
// const pq = new PriorityQueue((a, b) => b.val - a.val); <---- max-heap
36-
37-
// Initialize the priority queue with the first node from each list
38-
for (let list of lists) {
39-
if (list) pq.enqueue(list); // adds the element to the back (or tail) of the queue
40-
}
41-
42-
let dummy = new ListNode(-1);
43-
let current = dummy;
44-
45-
// Pop nodes from the priority queue and add them to the merged list
46-
while (!pq.isEmpty()) {
47-
const node = pq.dequeue(); // removes the element from the front (or head) of the queue
48-
current.next = node;
49-
current = current.next;
50-
51-
// If the current node has a next node, enqueue it
52-
if (node.next) pq.enqueue(node.next);
53-
}
54-
55-
return dummy.next;
56-
};
57-
58-
/**
59-
* β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
60-
* β”‚ Time and Space Complexity β”‚
61-
* β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
62-
* β”‚ Time Complexity: O(N log K) β”‚
63-
* β”‚ - N is the total number of nodes across all lists. β”‚
64-
* β”‚ - K is the number of lists. β”‚
65-
* β”‚ - Enqueueing and dequeueing each node from the priority queue takes O(log K) time. β”‚
66-
* β”‚ - Since there are N nodes in total, the overall time complexity is O(N log K). β”‚
67-
* β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
68-
* β”‚ Space Complexity: O(K) β”‚
69-
* β”‚ - The space complexity is determined by the priority queue, which stores at most K nodes. β”‚
70-
* β”‚ - Therefore, the space complexity is O(K), where K is the number of lists. β”‚
71-
* β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
72-
* β”‚ Notes:
73-
* https://support.leetcode.com/hc/en-us/articles/360011833974-What-are-the-environments-for-the-programming-languages β”‚
74-
* β”‚ - JavaScript is run with the --harmony flag, enabling new ES6 features. β”‚
75-
* β”‚ - The lodash.js library is included by default in the environment. β”‚
76-
* β”‚ - For Priority Queue / Queue data structures, you may use: β”‚
77-
* β”‚ - datastructures-js/priority-queue version 5.4.0 β”‚
78-
* β”‚ - datastructures-js/queue version 4.2.3 β”‚
79-
* β”‚ - datastructures-js/deque version 1.04 β”‚
80-
* β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
81-
*/
82-
831
// Brute Force (Array Sorting) - Good for smaller cases
842
// πŸ•’ Time Complexity: O(N log N), where N is the total number of nodes
853
// πŸ—‚οΈ Space Complexity: O(N)

0 commit comments

Comments
Β (0)