Skip to content

Commit

Permalink
Fix merge-k-sorted-lists solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeehay28 committed Feb 15, 2025
1 parent 62212e2 commit 203ebfd
Showing 1 changed file with 0 additions and 82 deletions.
82 changes: 0 additions & 82 deletions merge-k-sorted-lists/Jeehay28.js
Original file line number Diff line number Diff line change
@@ -1,85 +1,3 @@
// Priority Queue (Min-Heap) - Best for larger cases
// πŸ•’ Time Complexity: O(N log K), where N is the total number of nodes, K is the number of lists
// πŸ—‚οΈ Space Complexity: O(K)

//
/** const PriorityQueue = require('datastructures-js').PriorityQueue;
* // 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.
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = val === undefined ? 0 : val;
* this.next = next === undefined ? null : next;
* }
*/
/**
* @param {ListNode[]} lists
* @return {ListNode}
*/

var maxDepth = function (root) {
// Base case: if the node is null, the depth is 0
if (root === null) return 0;

// Recursively calculate the depth of the left and right subtrees
let leftDepth = maxDepth(root.left);
let rightDepth = maxDepth(root.right);

// Return the maximum of the two depths plus 1 for the current node
return Math.max(leftDepth, rightDepth) + 1;
};

var mergeKLists = function (lists) {
// Create a priority queue (min-heap) to store the nodes
const pq = new PriorityQueue((a, b) => a.val - b.val);
// const pq = new PriorityQueue((a, b) => b.val - a.val); <---- max-heap

// Initialize the priority queue with the first node from each list
for (let list of lists) {
if (list) pq.enqueue(list); // adds the element to the back (or tail) of the queue
}

let dummy = new ListNode(-1);
let current = dummy;

// Pop nodes from the priority queue and add them to the merged list
while (!pq.isEmpty()) {
const node = pq.dequeue(); // removes the element from the front (or head) of the queue
current.next = node;
current = current.next;

// If the current node has a next node, enqueue it
if (node.next) pq.enqueue(node.next);
}

return dummy.next;
};

/**
* β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
* β”‚ Time and Space Complexity β”‚
* β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
* β”‚ Time Complexity: O(N log K) β”‚
* β”‚ - N is the total number of nodes across all lists. β”‚
* β”‚ - K is the number of lists. β”‚
* β”‚ - Enqueueing and dequeueing each node from the priority queue takes O(log K) time. β”‚
* β”‚ - Since there are N nodes in total, the overall time complexity is O(N log K). β”‚
* β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
* β”‚ Space Complexity: O(K) β”‚
* β”‚ - The space complexity is determined by the priority queue, which stores at most K nodes. β”‚
* β”‚ - Therefore, the space complexity is O(K), where K is the number of lists. β”‚
* β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
* β”‚ Notes:
* https://support.leetcode.com/hc/en-us/articles/360011833974-What-are-the-environments-for-the-programming-languages β”‚
* β”‚ - JavaScript is run with the --harmony flag, enabling new ES6 features. β”‚
* β”‚ - The lodash.js library is included by default in the environment. β”‚
* β”‚ - For Priority Queue / Queue data structures, you may use: β”‚
* β”‚ - datastructures-js/priority-queue version 5.4.0 β”‚
* β”‚ - datastructures-js/queue version 4.2.3 β”‚
* β”‚ - datastructures-js/deque version 1.04 β”‚
* β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
*/

// Brute Force (Array Sorting) - Good for smaller cases
// πŸ•’ Time Complexity: O(N log N), where N is the total number of nodes
// πŸ—‚οΈ Space Complexity: O(N)
Expand Down

0 comments on commit 203ebfd

Please sign in to comment.