Тёмный

Merge K Sorted Lists - Divide and Conquer Approach 

AlgosWithMichael
Подписаться 21 тыс.
Просмотров 18 тыс.
50% 1

Опубликовано:

 

23 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 62   
@hurfyd
@hurfyd 3 года назад
A small optimization I prefer in the merge function: in the while loop instead check (if l1 && l2) this will keep merging the two lists while both have items. Then afterwards we know that one of the lists could still have items. Append all the items with something like result.next = l1 || l2
@AlgosWithMichael
@AlgosWithMichael 3 года назад
Ah very cool optimization, thank for sharing
@Rpkerr999
@Rpkerr999 3 года назад
One of the best explanations w/ Divide and Conquer I've seen for this problem, thank you!
@AlgosWithMichael
@AlgosWithMichael 3 года назад
You're very welcome!
@Aditya-jv9mp
@Aditya-jv9mp 3 года назад
I just did Merge sort in college, this video explained it so well!!
@AlgosWithMichael
@AlgosWithMichael 3 года назад
Awesome, glad to hear that!
@bhairavas2528
@bhairavas2528 3 года назад
You Awesome bro. not only you would solve the problem. Also with Time and space complexity. Most of the RU-vidrs would not give interest in these complexities. Please keep posted videos. Do not stop bro. if possible post more than 1 video in a week
@AlgosWithMichael
@AlgosWithMichael 3 года назад
Thank you very much! And I'm trying to get more videos out, working a full time job cuts into most of the RU-vid time :/
@timk9977
@timk9977 2 года назад
Thanks for the video, you explained it really well. I am a bit confused about final time complexity. It's log k levels, yes, but on every level the list size doubles, so we still have to go through all of the elements every time. Say, we have 4 lists, each n long; at the bottom we do 2 merges, of length 2n each, that's 4n elements to look at. At the top, we now merge these two lists, each is 2n long, so we have to go thru 4n elements (again). Overall we've looked at the elements 8n times, which is O(nk), not O(n log(k)). It would be faster (only 4n times) if we simply merged the lists sequentially. How are we benefiting from divide and conquer here? I am probably missing something here, would appreciate any comments.
@dambar67
@dambar67 2 года назад
here N means total elements of all k lists which is n*k
@lizze9131
@lizze9131 3 года назад
Thanks so much. Really like your video and the way you explain these problems. It is clear, detailed and easy to understand :)
@AlgosWithMichael
@AlgosWithMichael 3 года назад
Great to hear!
@keleshi4894
@keleshi4894 2 года назад
Great video. One minor thing though, we don't really need l2 = l2.next or l1 = l1.next in line 31 and line 34 right, we can just break out of the loop and not worry about them since the remaining of them are already sorted in a linkedlist?
@mohamedabdul633
@mohamedabdul633 3 года назад
Dude, I got tell ya this tutorial is high class.
@AlgosWithMichael
@AlgosWithMichael 3 года назад
Hahah thanks man, appreciate it!
@LegitGamer2345
@LegitGamer2345 3 года назад
In the description u have written space complexity is O(LOGK) and in the video u said it’s O(K) , the correct answer is logK right ?
@AlgosWithMichael
@AlgosWithMichael 3 года назад
Woops yea
@LegitGamer2345
@LegitGamer2345 3 года назад
@@AlgosWithMichael nice , btw thanks for the clean explanation
@AlgosWithMichael
@AlgosWithMichael 3 года назад
anyime!
@theghostwhowalk
@theghostwhowalk 4 года назад
Awesome!! Nicely explained. Asked in a lot of companies
@AlgosWithMichael
@AlgosWithMichael 4 года назад
Thank you very much! It is definitely a useful problem to learn
@theghostwhowalk
@theghostwhowalk 4 года назад
@@AlgosWithMichael also your videos def helped me crack couple of Fangs! Please do let me know if you would like any contacts/ more info.
@kennettemaddela5682
@kennettemaddela5682 2 года назад
Clear and concise. Thank you!
@kunalgiri6114
@kunalgiri6114 4 года назад
Happy to see after long time.... Thanks!!!
@AlgosWithMichael
@AlgosWithMichael 4 года назад
Most welcome 😊
@StyleTrick
@StyleTrick 3 года назад
Thanks Michael, you've explained the logic really well! subscribed :)
@AlgosWithMichael
@AlgosWithMichael 3 года назад
Glad it was helpful!
@sitronco
@sitronco 4 года назад
I like yours videos man. Thank you so much for the fantastic material
@AlgosWithMichael
@AlgosWithMichael 4 года назад
Thank you, glad I could help!
@SheikhSadiruddin1
@SheikhSadiruddin1 4 года назад
Great explanation as always!!!
@AlgosWithMichael
@AlgosWithMichael 3 года назад
Glad you liked it!
@lassenordahl
@lassenordahl 3 года назад
Michael I had no idea you had a leetcode channel this is amazing I wish I knew about it before I did the grind this fall
@lassenordahl
@lassenordahl 3 года назад
Subscribed :)
@AlgosWithMichael
@AlgosWithMichael 3 года назад
Lol thanks man! Only a couple people at work know haha
@AlgosWithMichael
@AlgosWithMichael 3 года назад
@chiraggarg5297
@chiraggarg5297 2 года назад
Great explanation man!!
@AlgosWithMichael
@AlgosWithMichael 2 года назад
Thank you!
@Phoenix-xi2gy
@Phoenix-xi2gy 3 года назад
Awesome video man!! Amazing explanation
@AlgosWithMichael
@AlgosWithMichael 3 года назад
Appreciate it, more videos to come!
@yitingg7942
@yitingg7942 3 года назад
Thank you for your excellent explanation. Just one question, may I ask if space is O(1) or O(k)? I saw leetcode solution with O(1) space for this divide and conquer approach.
@AlgosWithMichael
@AlgosWithMichael 3 года назад
The space complexity for the recursive solution is O(k) where k is the recursion depth. The leetcode solution goes over the iterative approach which is O(1) space complexity. I went over the recursive approach because it is much easier to understand in my opinion :)
@yitingg7942
@yitingg7942 3 года назад
@@AlgosWithMichael Oh I got it. Thanks a lot Michael!
@AlgosWithMichael
@AlgosWithMichael 3 года назад
No problem!
@dambar67
@dambar67 2 года назад
i believe time complexity is O(n*klogk)
@alfredeben-foby1274
@alfredeben-foby1274 3 года назад
Do you think there is a way to solve this with a minHeap and still have a time complexity of nlogk? I have been able to solve it in nlogn time but I have had a few follow up questions with amazon/facebook where they ask how would you optimize your solution using a minHeap to a time complexity of nlogk Btw your videos are awesome, just subscribed.
@AlgosWithMichael
@AlgosWithMichael 3 года назад
Yes, to do nlogk you need to ensure only k elements are in your minheap at any given time. Maybe I can do a video on that approach
@hurfyd
@hurfyd 3 года назад
At the moment you might be iterating over every node in every list and putting them all into the heap. This means your heap space will be O(N) and the the time to insert and remove from the heap will be O(logN) (because you have N items in the heap). Instead you can keep only k elements in the heap at one time. This will reduce the space to O(k) and the insert/remove time to O(logk) Initially put only the heads of each list into the queue. Then when removing from your heap, check if the node you dequeued has a next node. If so enqueue that to the heap
@RanjuRao
@RanjuRao 3 года назад
good explanation :) Thumbs up! 👍
@AlgosWithMichael
@AlgosWithMichael 3 года назад
Awesome thanks!
@libitard
@libitard 3 года назад
I love this where you did not use a Queue.
@AlgosWithMichael
@AlgosWithMichael 3 года назад
Haha yea I love this approach as well
@chessmaster856
@chessmaster856 Год назад
Priority q would work in n log k and more straight forward.
@parteeksatija5536
@parteeksatija5536 4 года назад
Sir can you explain a little bit more. What is K in O(N*log(K))?
@AlgosWithMichael
@AlgosWithMichael 3 года назад
K is the amount of times we must "divide" our array. So in other words, it is how many times we call our recursive function
@parteeksatija5536
@parteeksatija5536 3 года назад
@@AlgosWithMichael Ok got it. thanks
@leyocode8868
@leyocode8868 Год назад
why didnt you just fill a list by traversing every single node of the list of listnode and then sort it using collection.sort and then create node from that sorted list ?
@leyocode8868
@leyocode8868 Год назад
public ListNode mergeKLists(ListNode[] lists) { ListNode dummy_head = new ListNode(-1); ListNode result = dummy_head ; List memory= new ArrayList(); // filling my list for(int i =0 ; i< lists.length ; i ++ ){ while(lists[i]!= null){ memory.add(lists[i].val); lists[i]=lists[i].next; } } Collections.sort(memory); for(int k : memory){ dummy_head.next = new ListNode(k); dummy_head = dummy_head.next ; } return result.next; }
@madhavsomaniofficial
@madhavsomaniofficial 3 года назад
Priority queue to the rescue.
@AlgosWithMichael
@AlgosWithMichael 3 года назад
Yep it is
Далее
Decode String | FAANG Coding Question | Stack
17:03
Просмотров 10 тыс.
10 FORBIDDEN Sorting Algorithms
9:41
Просмотров 898 тыс.
Кольцо Всевластия от Samsung
01:00
Просмотров 664 тыс.
Amazon Coding Question - Insert Delete GetRandom O(1)
11:54
Harder Than It Seems? 5 Minute Timer in C++
20:10
Просмотров 188 тыс.
Interview Question: Merge K Sorted Arrays
26:46
Просмотров 61 тыс.
How I would learn Leetcode if I could start over
18:03
Просмотров 625 тыс.
Dijkstra's Algorithm - Computerphile
10:43
Просмотров 1,3 млн
Word Ladder | Breadth First Search (LeetCode)
15:08
Просмотров 13 тыс.
Кольцо Всевластия от Samsung
01:00
Просмотров 664 тыс.