Тёмный

Merge K Sorted Arrays - Min Heap Algorithm ("Merge K Sorted Lists" on LeetCode) 

Back To Back SWE
Подписаться 241 тыс.
Просмотров 89 тыс.
50% 1

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

 

9 сен 2024

Поделиться:

Ссылка:

Скачать:

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

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 270   
@BackToBackSWE
@BackToBackSWE 5 лет назад
Table of Contents: Dissing A Binder I Found 0:00 - 0:08 The Problem Introduction 0:08 - 1:06 Merge 2 Sorted Arrays (The Intuition) 1:06 - 3:18 Merge K Sorted Arrays (The Intuition Scaled) 3:18 - 5:11 Cool but...How Do We Code This? 5:11 - 6:09 Walkthrough With A Min Heap 6:09 - 8:47 Time Complexity 8:47 - 10:26 Space Complexity 10:26 - 10:44 Subscriber Begging 10:44 - 11:05 The code for this solution is in the description. Although for arrays, it can easily be adapted and applied to work on any type of list (like a Linked List).
@BackToBackSWE
@BackToBackSWE 5 лет назад
@Trinitrophenylnitramine Yeah I'm a student right now and yeah you learn algorithms and data structures very fast but for me pretty much all of these questions are from self studying. And great, I wish you luck!
@ryanchadwick4705
@ryanchadwick4705 3 года назад
I can't find the code in the description
@EE12345
@EE12345 3 года назад
@Leonardo Elisha No one gives a shit, reported
@nafessajaigirdar6651
@nafessajaigirdar6651 4 года назад
i love how outspoken and clear you are with the camera. You make me feel bad for going my phone and getting distracted lmao
@BackToBackSWE
@BackToBackSWE 4 года назад
thx haha
@jubinsoni4694
@jubinsoni4694 4 года назад
you are amazing bro...not only you explained the solution but you also the way in which we need to think during the interview
@BackToBackSWE
@BackToBackSWE 4 года назад
thanks
@mohdt786
@mohdt786 4 года назад
People would actually think of either Merge sort or Heap sort which would result into N*log(N) complexity but this technique actually made use of the sorted arrays, implemented both sorting techniques and reduced the complexity to N*log(K). Amazing tutor. Thank you!
@BackToBackSWE
@BackToBackSWE 4 года назад
sure
@Makwayne
@Makwayne 3 года назад
When you start whispering it almost feels like you're about to start singing "all of me"
@Nirvana4u-n4w
@Nirvana4u-n4w 5 лет назад
Best part of this video is incremental approach. This is what is required during high pressure interview.
@BackToBackSWE
@BackToBackSWE 5 лет назад
I'm slowly learning to teach better. I think the key to these questions is just that. The solution will not come often. You must build on what you know to get there. That is how I wanted to teach this problem and how I approach all my teachings. Thanks for dropping by.
@dazzle5350
@dazzle5350 5 лет назад
Duuuuuude, you're amazing. This is exactly what I needed for my Datastructures-Homework. Thanks a lot man!
@BackToBackSWE
@BackToBackSWE 5 лет назад
sure
@techwithitchris
@techwithitchris 4 года назад
This helped me a lot. Thank you.
@BackToBackSWE
@BackToBackSWE 4 года назад
nice
@samarthbhandari1360
@samarthbhandari1360 5 лет назад
Loved how you built the problem from the ground up! Wonderful
@BackToBackSWE
@BackToBackSWE 5 лет назад
Thank you. Thanks for watching.
@adityamallik3064
@adityamallik3064 Месяц назад
This was a fantastically, clear explanation. Thank you so much!
@suraj8087
@suraj8087 5 лет назад
You are helping a lot more than you think you are. Thanks Man.
@BackToBackSWE
@BackToBackSWE 5 лет назад
Yeah, I feel like no one watches
@jessicaresnick9628
@jessicaresnick9628 5 лет назад
love love love this video! Such a clear explanation and so much energy and enthusiasm!
@BackToBackSWE
@BackToBackSWE 5 лет назад
thanks lol, this video is old
@jugrajsingh1273
@jugrajsingh1273 4 года назад
This video calmed me dude , it was smooth af...the way you explain is amazing
@BackToBackSWE
@BackToBackSWE 4 года назад
nice haha
@sachinsaini1806
@sachinsaini1806 4 года назад
I don't understand when you are not yelling, and it is actually not yelling i can see your passion when you speak with your heart. Keep it up bro :D
@BackToBackSWE
@BackToBackSWE 4 года назад
I'm in a library haha. Old video - before I ever got a mic.
@sikaruplays2999
@sikaruplays2999 4 года назад
Crystal clear explanation👍👍👍
@BackToBackSWE
@BackToBackSWE 4 года назад
thx
@Erickelnd
@Erickelnd 2 года назад
Almost 5 years studying Computer Science and this is the video I could best understand so far ¡You are amazingly good at this!
@BackToBackSWE
@BackToBackSWE 2 года назад
Thank you, glad you liked it 😀 Do check out backtobackswe.com/platform/content and please recommend us to your family and friends 😀
@pimpmobile599
@pimpmobile599 5 лет назад
Awesome video! I really like the way you broke down the time complexity for this solution
@BackToBackSWE
@BackToBackSWE 5 лет назад
sure
@sarahan7864
@sarahan7864 3 года назад
Thanks!! This video helped me a lot understanding k-way merge sort. Hoping to see more great videos in the future 👍👍👍
@BackToBackSWE
@BackToBackSWE 3 года назад
will do
@vibhoragarwal5425
@vibhoragarwal5425 4 года назад
point to point explanation very nice content hats off to you man from india
@BackToBackSWE
@BackToBackSWE 4 года назад
thanks - hey from america
@BryanAndradeNYC
@BryanAndradeNYC 5 лет назад
Thanks for the video! Also the time/space complexity explanation at the end was helpful.
@BackToBackSWE
@BackToBackSWE 5 лет назад
sure
@Jesus.Christ..
@Jesus.Christ.. 4 года назад
This guy is the best for the algorithm test
@BackToBackSWE
@BackToBackSWE 4 года назад
thx
@vimarshkoul930
@vimarshkoul930 4 года назад
This is a great video. The concept explained here was once used for patience sort. Thanks!
@BackToBackSWE
@BackToBackSWE 4 года назад
Thanks and nice and sure
@casinarro
@casinarro Год назад
That's such a nice neat and involving explanation. You make sense all the time! Thanks.
@jomosis9234
@jomosis9234 3 года назад
Thanks for your extremely clear explanation for such kind of problems. Especially your time and space analysis really helps me a lot.
@ichdiegross
@ichdiegross 5 лет назад
Love all your work! I always look for your videos if I'm stuck on a Leetcode problem.
@BackToBackSWE
@BackToBackSWE 5 лет назад
Nice, I wish I could do all 800+ of them haha
@ichdiegross
@ichdiegross 5 лет назад
😂😂😂
@RAJATTHEPAGAL
@RAJATTHEPAGAL 4 года назад
Just found ur channel, your explanations are absolutely awesome ....
@BackToBackSWE
@BackToBackSWE 4 года назад
great welcome
@vasundhramanhas7884
@vasundhramanhas7884 5 лет назад
excellent explanation!!!! u r amazing This sure will be the largest and the best resource for all the programmers in the coming years. Cant believe two morons disliked this video.I am so angry at them.
@BackToBackSWE
@BackToBackSWE 5 лет назад
haha thanks
@elliotho3015
@elliotho3015 2 года назад
You are amazing . Thank you for your sharing!
@dimaifoxnetworks6332
@dimaifoxnetworks6332 5 лет назад
Really helpful and explicit! Keep it up!
@BackToBackSWE
@BackToBackSWE 5 лет назад
thanks
@peerless3538
@peerless3538 2 года назад
Awww you have helped me a lot, was exploring this solution for a long time but didn't find a preety clear and explained vdo on RU-vid, then I got your vdo really awesome man:) You deserve my subscribe 🙌🏻
@BackToBackSWE
@BackToBackSWE 2 года назад
Thanks! Appreciate it :) Are you preparing for an interview?
@peerless3538
@peerless3538 2 года назад
@@BackToBackSWE Yes I am preparing for an internship but I am afraiding of lots, I would get it or not 😐 also create a video on how to get a referral for product companies plz 🙃
@Avinashkumar-xt4zq
@Avinashkumar-xt4zq 4 года назад
loved your way of explanation man!! great video
@BackToBackSWE
@BackToBackSWE 4 года назад
thanks
@yuvrajoberoi7834
@yuvrajoberoi7834 4 года назад
This was brilliant. Wish you reach 100k fast and grow even beyond.
@BackToBackSWE
@BackToBackSWE 4 года назад
thanks!
@divyampatro8586
@divyampatro8586 3 года назад
Very lucid! Thanks :)
@dhruvilprajapati4734
@dhruvilprajapati4734 Год назад
Lucid explanation man
@architshinde3977
@architshinde3977 4 года назад
This video really helped me a lot! Thank you :)
@BackToBackSWE
@BackToBackSWE 4 года назад
great!
@realtechbro7982
@realtechbro7982 5 лет назад
interviewing tomorrow @ Tsla. your step by step procedures are a god send. I got u
@BackToBackSWE
@BackToBackSWE 5 лет назад
nice
@NathanJohnson815
@NathanJohnson815 4 года назад
how did it go?
@BackToBackSWE
@BackToBackSWE 4 года назад
@@NathanJohnson815 chill lol
@JunLiLin0616
@JunLiLin0616 Год назад
Appreciate a lot! You're just amazing. Pretty clear explanation. Thanks a lot for rescuing me from my homework lol
@cookval
@cookval 4 года назад
Thanks for breaking this down!
@BackToBackSWE
@BackToBackSWE 4 года назад
sure
@vivekgr3001
@vivekgr3001 4 года назад
really liked the way you explained. Well done!
@BackToBackSWE
@BackToBackSWE 4 года назад
thx
@sujoyseal195
@sujoyseal195 3 года назад
This helped . Come up with the code walk through some day.
@joonsangpark8351
@joonsangpark8351 4 года назад
thanks! it was a big help
@BackToBackSWE
@BackToBackSWE 4 года назад
sure
@tirthjayswal9895
@tirthjayswal9895 4 года назад
Awsome Intuition
@BackToBackSWE
@BackToBackSWE 4 года назад
thx
@sophiagigliotti707
@sophiagigliotti707 3 года назад
Thank you so much, this video is really helpful. You are a talented teacher, so refreshing to find in tech!
@mushfiqrashid
@mushfiqrashid 3 года назад
You're a lifesaver my man!
@prernachauhan6262
@prernachauhan6262 4 года назад
You are amazing! Made it so easy!
@BackToBackSWE
@BackToBackSWE 4 года назад
thanks!
@shriyadixit5571
@shriyadixit5571 3 года назад
Dude I loved ur explanation thanks a lot it really helped a ton. U make it so clear and easy.
@nupurjaiswal957
@nupurjaiswal957 3 года назад
Best explanation of time complexity!
@dmsehgal87
@dmsehgal87 5 лет назад
awsome. thanks! Please keep at it for more.
@BackToBackSWE
@BackToBackSWE 5 лет назад
Working on it. Love.
@shikharbansal2942
@shikharbansal2942 2 года назад
Amazing tutorial thank!
@kennethmensah4480
@kennethmensah4480 2 года назад
You explained this so clearly
@aakashjain3498
@aakashjain3498 4 года назад
Thanks a lot man for that explanation.
@BackToBackSWE
@BackToBackSWE 4 года назад
sure
@StuBz211
@StuBz211 4 года назад
Amazing, that video is really help to understand. Thank you!
@BackToBackSWE
@BackToBackSWE 4 года назад
sure
@ahmadm4049
@ahmadm4049 5 лет назад
Thumbs up! fantastic explanation
@BackToBackSWE
@BackToBackSWE 5 лет назад
thx
@jay-rathod-01
@jay-rathod-01 4 года назад
Beautiful explanation.
@BackToBackSWE
@BackToBackSWE 4 года назад
thanks
@shalinsitwala
@shalinsitwala 4 года назад
Very helpful, thanks!
@BackToBackSWE
@BackToBackSWE 4 года назад
sure
@somedude1295
@somedude1295 4 года назад
I love you work man, you are such a great teacher
@BackToBackSWE
@BackToBackSWE 4 года назад
thx
@Raj_Patel21
@Raj_Patel21 5 лет назад
Awesome, man!!!!
@BackToBackSWE
@BackToBackSWE 5 лет назад
hey
@phanichoragudi57
@phanichoragudi57 5 лет назад
Wonderful explanation! Keep making more 😁
@BackToBackSWE
@BackToBackSWE 5 лет назад
working on it
@kylenichols5256
@kylenichols5256 3 года назад
great video man
@harmeetbindra6978
@harmeetbindra6978 5 лет назад
I really like your explanations
@BackToBackSWE
@BackToBackSWE 5 лет назад
thanks
@SuperPabbs
@SuperPabbs 4 года назад
Thank you, Ben.
@BackToBackSWE
@BackToBackSWE 4 года назад
sure lol
@raturimic
@raturimic 5 лет назад
thanks for the video , one small suggestion: dont move rapidly in front of camera , specially when the focus is too close . that will blur the images and kind of discomfort to viewer .
@BackToBackSWE
@BackToBackSWE 5 лет назад
ok, I'll go back in time and fix this video, give me onnnnne sec 🏃💨💨🏃💨💨
@BackToBackSWE
@BackToBackSWE 5 лет назад
ok, back, this video should be fixed.
@user-oy4kf5wr8l
@user-oy4kf5wr8l 3 года назад
We love u buddy 😄
@darshansimha2166
@darshansimha2166 4 года назад
Beautiful explanation!
@BackToBackSWE
@BackToBackSWE 4 года назад
thx
@shreyanshgupta6887
@shreyanshgupta6887 4 года назад
really a great explanation man
@BackToBackSWE
@BackToBackSWE 4 года назад
ye
@akankshasharma148
@akankshasharma148 3 года назад
Greatly explained...Thanks man👌
@ramizrizwan3057
@ramizrizwan3057 Год назад
such a good video-- you're the best
@BackToBackSWE
@BackToBackSWE Год назад
Really glad to help 🎉 Do you know about our 5-Day Free DSA Mini Course? Check it out here - backtobackswe.com/
@atulbhardwaj6288
@atulbhardwaj6288 3 года назад
Great video bro.
@saravananj3898
@saravananj3898 2 года назад
You explained it damn clear !!!
@Kermitnirmit
@Kermitnirmit 5 лет назад
Solid video! Thanks!
@BackToBackSWE
@BackToBackSWE 5 лет назад
hey, thanks
@basiltinasage6282
@basiltinasage6282 3 года назад
Thanks, very good explained!
@amando96
@amando96 5 лет назад
My first lazy approach was to merge them without giving a fuck and then just quicksorting, which allowed me to reuse the quicksort function the interviewer already asked for previously.
@BackToBackSWE
@BackToBackSWE 5 лет назад
yeah, you can start like that
@kitube100
@kitube100 3 года назад
Amazing!!! Just Amazing!
@BackToBackSWE
@BackToBackSWE 3 года назад
thanks
@sakshamsrivastava6280
@sakshamsrivastava6280 3 года назад
this one was great !
@BackToBackSWE
@BackToBackSWE 3 года назад
thx
@jishulayek8252
@jishulayek8252 5 лет назад
Awesome... Really liked it.
@BackToBackSWE
@BackToBackSWE 5 лет назад
thanks
@kartiksaini5619
@kartiksaini5619 3 года назад
omg omg damn man just loved it n...................the best ..........thx alot
@estynussbaum6506
@estynussbaum6506 5 лет назад
Thankkkkkk youuu!!!!! These videos really help
@BackToBackSWE
@BackToBackSWE 5 лет назад
sure
@hmrishav
@hmrishav 3 года назад
I accidentally watched this in 2x like I generally do your videos. Boy, I am watching at 0.75 now.
@fantasy9960
@fantasy9960 2 года назад
thank you, sir. you explained this topic very clearly !!!
@BackToBackSWE
@BackToBackSWE Год назад
Thanks!
@wangrobbie6867
@wangrobbie6867 4 года назад
professional Work!
@BackToBackSWE
@BackToBackSWE 4 года назад
thank you.
@user-gx9lr2mh2i
@user-gx9lr2mh2i 3 года назад
great video
@FouadBallan
@FouadBallan 5 лет назад
You are the best man . subd !!
@BackToBackSWE
@BackToBackSWE 5 лет назад
hey
@adrianmicu7552
@adrianmicu7552 4 года назад
Thanks man!
@BackToBackSWE
@BackToBackSWE 4 года назад
sure
@bfyou
@bfyou 3 года назад
Yo bro no homo but that mustache and beard line up looking clean!! Thanks for the help!!
@pabloarkadiusz4687
@pabloarkadiusz4687 3 года назад
Hey, couldn't be done just inserting all elements from all arrays in the heap and just pop them all ?
@BackToBackSWE
@BackToBackSWE 3 года назад
yes, but you want it to be iterative, inserting all n items and removing each would be n log(n) insertions then n log(n) removals so O(n * logn) vs O(n * log(k)) where k is the total arrays to be merged. When k
@pabloarkadiusz4687
@pabloarkadiusz4687 3 года назад
@@BackToBackSWE Thanks !!
@akhashr
@akhashr 4 года назад
Great. Kudos.
@BackToBackSWE
@BackToBackSWE 4 года назад
thx
@MunniDivya
@MunniDivya 4 года назад
Great explaination. Merge sort algorithm internally uses 2 pointer algorithm for 2 sorted arrays. For K arrays use min heap concept which is an extension to 2 pointer algorithm for getting the least element from the heap . Thats what you have told correct ? Thanks :D
@BackToBackSWE
@BackToBackSWE 4 года назад
im not sure dont remmber the video
@MunniDivya
@MunniDivya 4 года назад
@@BackToBackSWE hehe okay
@tarunbhutani9604
@tarunbhutani9604 4 года назад
thanks brother..
@BackToBackSWE
@BackToBackSWE 4 года назад
sure
@abhirupbandyopadhyay4191
@abhirupbandyopadhyay4191 2 года назад
thanks bro very nicely taught,but didnt found "code" in description.
@pl5778
@pl5778 5 лет назад
great stuff
@BackToBackSWE
@BackToBackSWE 5 лет назад
hey
@niyatikhandelwal7017
@niyatikhandelwal7017 Год назад
Good work
@BackToBackSWE
@BackToBackSWE Год назад
Thank you 🎉 Please enjoy a special coupon from us - backtobackswe.com/checkout?plan=lifetime-legacy&discount_code=SUB 🚀
@ria_bomma
@ria_bomma Год назад
shouldn't the time complexity be O(k + 2n*log(k)), where the additional k accounts for the time it takes to build the initial min-heap structure? because the video only accounts for the insertion and deletion of each element from the heap. unless of course you are assuming that k is dominated by 2n*log(k) in which case you can omit it...
@maldharm
@maldharm 5 лет назад
Thank you :)
@BackToBackSWE
@BackToBackSWE 5 лет назад
sure, no problem
@amith89rm
@amith89rm 4 года назад
Explanation is pretty amazing but too much movement in front of the camera causes discomfort to the viewer.
@BackToBackSWE
@BackToBackSWE 4 года назад
yeah old video sorry about that
@codeblooded6760
@codeblooded6760 4 года назад
How will you name the pointers to arrays when number of input arrays is k?
@BackToBackSWE
@BackToBackSWE 4 года назад
wym?
@codeblooded6760
@codeblooded6760 4 года назад
@@BackToBackSWE 7:05 how does the heap know from which array we got the element? Should we keep array of k pointers each point to the current element which we are comparing and then incrementing like kpointer[0]++ to check next element of that array?
@aagamjain5063
@aagamjain5063 3 года назад
Why is space complexity O(k) and not O(n+k), we are using an array of size of n to store the final values?
@siddharthb2226
@siddharthb2226 2 года назад
class Element { int value; int row; int index; public Element(int value, int row, int index) { this.value = value; this.row = row; this.index = index; } } class Solution { //Function to merge k sorted arrays. public static ArrayList mergeKArrays(int[][] arr,int K) { // Write your code here. ArrayList res = new ArrayList(); //Declaration of Min Heap, which sorts based on its value PriorityQueue min_heap = new PriorityQueue(new Comparator() { public int compare(Element a, Element b) { // if both the values are same then compare on the basis of their index if((a.value - b.value) == 0) return a.index - b.index; return a.value - b.value; } }); //Basis Step -> The first element of each row must be added to the min heap from where we can perform our calculation for(int i = 0; i < arr.length; i++) { if(arr[i].length > 0) { min_heap.add(new Element(arr[i][0], i, 0)); } } /*From each iteration we select the minimum element present in the heap, and whichever element we selected, we pop it from heap and then we need to add the next element from that array from which we selected an element (from the 2D array) into our heap*/ while(!min_heap.isEmpty()) { Element t = min_heap.poll(); res.add(t.value); int newIndex = t.index + 1; if(newIndex != arr[t.row].length) { min_heap.add(new Element(arr[t.row][newIndex], t.row, newIndex)); } } return res; } }
@siddharthb2226
@siddharthb2226 2 года назад
You're welcome
@a.yashwanth
@a.yashwanth 4 года назад
It's weird when I watch you talk slowly after getting-used-to yelling.😊
@BackToBackSWE
@BackToBackSWE 4 года назад
lol
@PepeTostado
@PepeTostado 2 года назад
When you say annotate, do you mean like using a tuple? Or what is it? Thx
@dongshuowu3454
@dongshuowu3454 5 лет назад
wonderful
@BackToBackSWE
@BackToBackSWE 5 лет назад
yo
Далее
10 FORBIDDEN Sorting Algorithms
9:41
Просмотров 853 тыс.
Merge K Sorted Lists - Divide and Conquer Approach
12:01
Apple Event - September 9
1:38:19
Просмотров 21 млн
гендер пати🩷🩵
00:21
Просмотров 79 тыс.
Naming Things in Code
7:25
Просмотров 2,1 млн
Apple Event - September 9
1:38:19
Просмотров 21 млн