Merge K Sorted Lists

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

LMK if there's anything I can do to help you guys!

KevinNaughtonJr
Автор

Instead of putting all the elements in the priorityQueue, u can just put the first elements of every linkedList, and add a comparator and then while retrieving, add the next node of the retrieved node and continue till the queue is empty, this will call heapify only for k nodes at any time, so time complexity will be O(nlogk)

divyatejaswinivengada
Автор

What a humble person you are - "If I can do it, you can do it " Even after having so much experience.... Thank u for encouraging !!

prasadm
Автор

I got this question asked during an Amazon tech interview and i gave the wrong answer with confidence 😭

guiltyconscious
Автор

Great explanation in less than 5 min for an LC hard question Kevin. However interviewers are expecting the best solution, that is divide and conquer. If we go with this when asked in interview, they would definitely ask to optimize it.

nikkig
Автор

I watched 2 other vids and was still left confused. this was the easiest most no bs straight up explanation and solution

Bamtanbale
Автор

I think you can do it faster. You won’t need heap of size N(total number of elements) but just a heap of size K (number of lists) so complexity will be NlogK
Building a heap of size N is essentially brute forcing it. Better make an array and sort it and build a list. It would still be NlogN and won’t require the heap at all.

siddharthpathak
Автор

It could be hard because interviewer might have follow up questions like
1. How can you use the sorted property of each list?
2. Can it be done just by changing the pointers without creating a whole new list?
3. Solution with O(1) space?

For the Leetcode compiler it might bot be hard, but for a real human interviewer, it could be really hard for an interviewee.

nishatsayyed
Автор

Really love that cover sound at the begging and at the end! Seems like that's number one reason why I'm watching your videos haha :)

djfedor
Автор

bro is a genius. i didn't understand after doing days of research. i watch his 5 min video and all of a sudden i get it.

ChristinaaDanks
Автор

This is the brute-force solution. There is a more optimum solution, where you don't throw everything in the heap.

theAnalyticGuy
Автор

@Kevin Naughton Jr. Your approach is actually solves the problem in O(n log n) which is not in-place. The problem in solving this way is that we are not using any benefit of having the lists of List nodes as sorted. This problem becomes hard when we have to solve the problem in O(n log K ) where K corresponds to size of the items present in the list provided. And if solve the problem with in- place algorithm, it becomes more difficult.

abhyudayauitrgpv
Автор

Solving this problem using minHeap data structure is an excellent idea Kevin. Kudos!

gopalgowda
Автор

I m wondering if the interviewer would buy this!😂😂

SR-wevl
Автор

for javascript user, you can just push all node values into an array and then sort the array: my solution using javasript:
var mergeKLists = function(lists) {
// basically we can grab all the numbers in an array, and sort that array,
// then we can create a new List, and keep making next nodes using the values in the sorted array
let arr = []
for (let i = 0; i <lists.length;i++){
let curNode = lists[i];
while(curNode !== null){
arr.push(curNode.val) // shift the first number into the arr, since it's smaller
curNode = curNode.next
}
}
// sort the arr ascending
arr = arr.sort((a, b) => a-b)
// console.log(arr)
let dummy = new ListNode(-1); // dummy node
let result = dummy // grab the head of dummy node list, so we can return result.next as our result
for ( let i=0;i<arr.length;i++ ){
dummy.next = new ListNode(arr[i])
dummy = dummy.next
}
return result.next
};

lifeincentralvalley
Автор

First time I didn't get it

Now I do. You are awesome.

benzeltser
Автор

best video ive found on this topic. surprised it could be this easy. I guess the hard part is realizing you can use a heap to simplify things.

snakeb
Автор

What solution is there if you’re not expected to use a min heap? When they are already sorted lists it’s expected to not use another data structure?

rydmerlin
Автор

Great video thank you so much. Love your style and simplicity.

TJKhara
Автор

Is using a Heap ok in an interview setting? I was told they want to see you do it with merge sort / divide & conquer

Historyiswatching