Merge k Sorted Lists | EP 14

  Рет қаралды 27,990

Fraz

Fraz

2 жыл бұрын

Lecture Notes:
Watch the complete Linked List Series • Linked List Series
Crio.Do
Get 10% discount Crio.do www.crio.do/redeem/7aef353
Question (codestudio) bit.ly/3CwTxDc
Question (leetcode) leetcode.com/problems/merge-k...
Placement Preparation Roadmap • Roadmaps for Placement
Hi, I am Fraz.
I am Software Engineer working at Curefit and I love teaching.
This is my Channel where I upload content related to Interviews and my personal experiences.
My contact details
Instagram :- / frazmohammad
Connect with me on LinkedIn :- / mohammad-f-bb4886157
Telegram Group t.me/LeadCoding

Пікірлер: 100
@chahatnagar1536
@chahatnagar1536 2 жыл бұрын
You've explained it so clearly! I was stuck on this question for a very long time. Thank you so much!
@mohammadfraz
@mohammadfraz 2 жыл бұрын
🔥 keep watching
@amanpandey4550
@amanpandey4550 Жыл бұрын
I tried with my own and my logic was correct but i failed to implement. I watched many videos but u cleared all my doubts. Thank u.
@bhavyagupta1389
@bhavyagupta1389 2 жыл бұрын
Your linked list series is amazing!!!
@mohammadfraz
@mohammadfraz 2 жыл бұрын
Thanks ❤️😊
@raghvendrakhatri5848
@raghvendrakhatri5848 2 жыл бұрын
Lect - 16 completed, sir love♥️ the way you explain each and every concept, please keep creating such wonderful content 💯 your playlist are helping me alot in understanding the topics and improving my skills. Thankyou bhaiya ♥️
@AmanChauhan-hr1wh
@AmanChauhan-hr1wh Жыл бұрын
I was thinking in same way but had difficulty in writing code for same...but u made it clear thank you so much
@abhi36292
@abhi36292 Жыл бұрын
great clean and concise code,Thanks
@technicalteam3298
@technicalteam3298 Жыл бұрын
you have such a great explanation
@stith_pragya
@stith_pragya 10 ай бұрын
Thank You So Much Fraz Brother for this wonderful video............🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
@AmarjeetKumar-en1gk
@AmarjeetKumar-en1gk 10 ай бұрын
Thanks for the comparator function explanation.
@panavkumar9009
@panavkumar9009 2 жыл бұрын
Please make the next playlist on heap. That is one tough topic to understand.
@user-jw9bt1nl7k
@user-jw9bt1nl7k 10 ай бұрын
I think the TC is O(n*k log k) lets say each list has 'n' nodes. to merge them we need n*k and to perform the divide and conquer part we need 'log k'
@vakhariyajay2224
@vakhariyajay2224 Жыл бұрын
Thank you very much. You are a genius.
@Music-tp8gg
@Music-tp8gg 2 жыл бұрын
Badhiya kaam krte ho faraz bhai
@ashishkashyap1872
@ashishkashyap1872 2 жыл бұрын
Amazing explanation
@jatingupta3443
@jatingupta3443 2 жыл бұрын
Completed till this lecture
@nimeshsingh6229
@nimeshsingh6229 2 жыл бұрын
nicely explained bhaiya !
@surajbaranwal56.
@surajbaranwal56. Жыл бұрын
Amazing playlist
@aditiranjan303
@aditiranjan303 2 жыл бұрын
Hard problem ko kitni easily solve kar dia😀
@asifhasan7444
@asifhasan7444 2 жыл бұрын
very good explanation
@I_Dont_Care_2bh
@I_Dont_Care_2bh Жыл бұрын
Brute Force:- Here, we are just storing all nodes sorted in 'multimap' then just construct a new List. (According to Fraz Bhaiya's 1st Approach) for naive approach refer this video.... #CODE:- class Solution { public: ListNode* mergeKLists(vector& lists) { multimapm; for(auto &it : lists){ ListNode *head = it; while(head){ m.insert(pair{head->val, head}); head = head->next; } } ListNode *dummy = new ListNode(-1); ListNode *tail = dummy; for(auto &it : m){ tail->next = it.second; tail = it.second; } return dummy->next; } };
@pranaykumar9433
@pranaykumar9433 2 жыл бұрын
comparator mein a->valval hota na
@ratnasanjay
@ratnasanjay 2 жыл бұрын
Thankyou bhaiya for this video
@shamimsarker839
@shamimsarker839 Жыл бұрын
Alhamdulillah, solved it by myself.
@kartikking7
@kartikking7 Жыл бұрын
bhaiya, comparator function mei a->val > b->val kyu aaya hai instead of a->valval, as we want it sorted in increasing order?
@shubhamkumar_noob_cs-go_pl581
@shubhamkumar_noob_cs-go_pl581 10 ай бұрын
why do we need a custom opearatorm while the deafault one greater is already available to use?
@sadashivshinde9150
@sadashivshinde9150 Жыл бұрын
Can we avoid cmp function by using priority_queue ?
@ankit_yadav11
@ankit_yadav11 9 ай бұрын
bhaiya every linkedlist ke 1st node ko kaise acces kar rhe ho aap list[i] karke . like hmko aisa same q ek tha merge sorted arrays to hmlog usme row and col store karke access karte the to pta lag pata tha ki alag alag array ka first element access kar rhe hai but yha kaise pta karenge withour row number ke kaise pta lagega ab dusra linkedlist aa gya only i use karke kaise ho rha yha
@sagarbora7768
@sagarbora7768 2 жыл бұрын
can we use simple global function instead of comparator class
@nitinkumarsingh7959
@nitinkumarsingh7959 2 жыл бұрын
nice, everything is clear.
@mohammadfraz
@mohammadfraz 2 жыл бұрын
Thanks
@saptarshimajumdar180
@saptarshimajumdar180 11 ай бұрын
thank you
@BobMarley-qh2qs
@BobMarley-qh2qs Жыл бұрын
bhai at 17:52 if we do ListNode* temp = q.top(); wouldn''t it be pointing to the whole list rather than a single element ? for example temp=q.top() / [1,3,4] how would it be pointing to a single element? and wouldn't the end result be: after everything done it would be like >> tail->[1,4,5]->[1,3,4]->[2,6]->[3,4]->[4,5]->[4]->[5]->[6] when it should be like >> tail->1->1->2->3->4->4->5->6;
@lakshaydubey3281
@lakshaydubey3281 Жыл бұрын
sir can we just insert all the elements of list on min heap and the keep extracting the min element until the head becomes null
@ishuchaudhary4502
@ishuchaudhary4502 2 жыл бұрын
How priority queue working here??Does it takes each element of linked list or only firsrt element of each linked list inside for loop ??can you pls explain q.push(v[i]) how this thing working inside priority queue
@satyamshukla1418
@satyamshukla1418 2 жыл бұрын
bhaiya please make video on comparators pls also your content on linkedlist is helping me a lot in my understanding of the concepts._/\_.
@Manish_Sahu
@Manish_Sahu Жыл бұрын
nice explanation
@rsp8053
@rsp8053 Жыл бұрын
Got doubt cleared
@Shivamkumar-xu6hu
@Shivamkumar-xu6hu 2 жыл бұрын
Very well explained 🔥
@mohammadfraz
@mohammadfraz 2 жыл бұрын
Thanks
@Shivamkumar-xu6hu
@Shivamkumar-xu6hu 2 жыл бұрын
@@mohammadfraz thanks to u bhaiya 🔥
@ianujkumark
@ianujkumark Жыл бұрын
appreciate it
@kartikkumar-6009
@kartikkumar-6009 2 жыл бұрын
i think divide and conquer is better approach for this or we can say merge sort
@SoHmmShorts
@SoHmmShorts 2 жыл бұрын
bhaiya compirator wala samajh nhi wala ..wo kahase parhu ? or uske liye kya refer karu ? plz help if anyone also know . thanks in advance
@SunnyGupta00
@SunnyGupta00 2 жыл бұрын
Reach++🔥
@BTECE_manichandT
@BTECE_manichandT Жыл бұрын
@fraz bhayya pls explain why (a->val > b->val )instead of (a->val < b->val)??
@tarunkumar.d8379
@tarunkumar.d8379 Жыл бұрын
Hi , Priority queue me sabse phele niche se fill hota hai, like low Priority Wale first fill hote hain, so agar a.val is greater than b.val ,that means uska prirority low hai, toh hum usse phele insert karne keliye return true karthe hain.
@manojg4451
@manojg4451 Жыл бұрын
bro, waiting for more videos. Please do more videos
@parasjaggi268
@parasjaggi268 2 жыл бұрын
great
@tech_wizard9315
@tech_wizard9315 2 жыл бұрын
By what date, entire series will be uploaded
@mohammadfraz
@mohammadfraz 2 жыл бұрын
Bro please jo hua hai vo dekh lo.
@ani68
@ani68 2 жыл бұрын
Sir when we do q.push(lists[i]) how only the first value of each list is pushed ??🤔🤔🤔🤔🤔🤔🤔🤔
@inFamous16
@inFamous16 2 жыл бұрын
Actually... initial vector that is passed to the function contains heads of all those k lists and once we have head, we can traverse the lists right? It is just that leetcode not shown this clearly in the problem statement itself, but it is a common sense thing.
@sageoustheory1957
@sageoustheory1957 Жыл бұрын
if(temp->next != NULL) pq.push(temp->next); what is the purpose oif this line
@pritishpattnaik4674
@pritishpattnaik4674 2 жыл бұрын
bhai I have a doubt , min heap ke syntax mein toh hum greater pass karwa sakte hai in place of that comparator function ? please clarify
@talkwithrd5697
@talkwithrd5697 2 жыл бұрын
I think if we pass greater then it won't give us correct answer, comparator function will work fine
@shashikalwar1853
@shashikalwar1853 2 жыл бұрын
@@talkwithrd5697 why so as result do not depend upon syntax
@inFamous16
@inFamous16 2 жыл бұрын
It is because ListNode* stores the address, not the values itself and how do you compare addresses?
@BTECE_manichandT
@BTECE_manichandT Жыл бұрын
@@inFamous16 it should be a->val < b->val na??pls explain
@inFamous16
@inFamous16 Жыл бұрын
@@BTECE_manichandT In C++ STL (by default) the largest element is at the top (i.e. at the last position), so that whenever we do pq.top(), we get highest priority element at first. Hence we have to do 'a->val > b->val' to get a min heap
@mdnadeem6343
@mdnadeem6343 2 жыл бұрын
🔥🔥🔥🔥🔥
@mohammadfraz
@mohammadfraz 2 жыл бұрын
Thanks
@niranjanraje7160
@niranjanraje7160 2 жыл бұрын
what if we push all the nodes in min heap and then keep poping out the minimum ??
@inFamous16
@inFamous16 2 жыл бұрын
as ListNode* is address type, by default the priority_queue is not going to sort them based on the address. We then have to push the values and then recreate linked_list from those values... but we have to do this in-place
@jitishgupta6424
@jitishgupta6424 Жыл бұрын
time complexity and SC at 12:30
@himanshugupta7647
@himanshugupta7647 2 жыл бұрын
Bhaiya product based company direct service based se hire karti h kya
@mohammadfraz
@mohammadfraz 2 жыл бұрын
Yes why not
@shreyanshjain4781
@shreyanshjain4781 2 жыл бұрын
bhiya please share your LinkedIn link , amazing explanation ,how bhiya you are you are able to think lots of approach , but i am not able to think more then 1-2 optimise solution approach
@ritikgupta1133
@ritikgupta1133 Жыл бұрын
practice
@ritikgupta1133
@ritikgupta1133 Жыл бұрын
see his leetcode profile you will be amazed that he has solve more then 1500 questions. nothing beats practice
@jaikrishan9100
@jaikrishan9100 10 ай бұрын
sir .TC ,SC kaise aaya samjh nahi aaya kuch.
@aayushojha3088
@aayushojha3088 2 жыл бұрын
we are inserting lisits[i] , isse to poora linked list insert hoga na ? but hame to bas 1st node insert krna thana ?
@tarunkumar.d8379
@tarunkumar.d8379 Жыл бұрын
Lists of i me sirf head pointer of that list hai bro, hum kabhi bhi pura LL pass nhi karthe, only the pointers are passed.
@naziyakhan4096
@naziyakhan4096 4 ай бұрын
All about Linkedlist I have understood is Curr node,next node, next.next node Return something is by default false I am not understanding anything I have come this far watching linkedlist, Where m I going wrong I am trying so much to understand But I am not understanding itself what is actually happening What to doo😢
@MdFaisal-kj1ck
@MdFaisal-kj1ck 2 жыл бұрын
Nice
@mohammadfraz
@mohammadfraz 2 жыл бұрын
Thanks Faisal
@MdFaisal-kj1ck
@MdFaisal-kj1ck 2 жыл бұрын
Kab aayega sir next ep?
@mohammadfraz
@mohammadfraz 2 жыл бұрын
Aagae hain
@kkumar07
@kkumar07 2 жыл бұрын
v[i] kya hai
@Sonu-tg6tg
@Sonu-tg6tg 2 жыл бұрын
Please make videos using JAVA
@jenil151
@jenil151 Жыл бұрын
18:10 can someone explain why we are pushing next of temp back in queue since temp will always be top element of queue
@BTECE_manichandT
@BTECE_manichandT Жыл бұрын
we want to compare the minimum element right ..so we push in to the heap until the next node is nullptr
@krishshah5162
@krishshah5162 Ай бұрын
cmp vala nhi smja kyu bnaya hai!?
@desihiphop4998
@desihiphop4998 2 жыл бұрын
Bhaiya 1 number bas heap ni aata tha search kiya phir hogya !!!!!!!
@mohammadfraz
@mohammadfraz 2 жыл бұрын
Niiice, yahi karna hota hai bro ❤️
@apurvtripathi7185
@apurvtripathi7185 Жыл бұрын
What is Time & Space Complexity ? ' class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNode dummy = new ListNode(0); ListNode curr = dummy ; while(l1 != null && l2 != null){ if(l1.val
@awwtawnoo
@awwtawnoo Жыл бұрын
class Solution { private: ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) { if (l1 == nullptr) return l2; if (l2 == nullptr) return l1; if (l1->val > l2->val) swap(l1, l2); ListNode *res = l1; while (l1 != nullptr && l2 != nullptr) { ListNode *temp = nullptr; while (l1 != nullptr && l1->val val) { temp = l1; l1 = l1->next; } temp->next = l2; swap(l1, l2); } return res; } public: ListNode *mergeKLists(vector &lists) { if (lists.empty()) return nullptr; int k = (int)(lists.size()); while (k > 1) { for (int i = 0; i < k / 2; i++) { lists[i] = mergeTwoLists(lists[i], lists[k - 1 - i]); } k = (k + 1) / 2; } return lists.front(); } }; not using heap, but better solution than just merging two consecutive ListNode-s from the ListNode vector
@tarunkumar.d8379
@tarunkumar.d8379 Жыл бұрын
Hi bro mujhe yeh smjh nhi ah rha hai ki lists front me kese answer agaya??
@awwtawnoo
@awwtawnoo Жыл бұрын
@@tarunkumar.d8379 In every iteration we are taking two lists and merging them to form the bigger list, at the end of all the iterations we need to return the HEAD of the finally formed list which is lists.front( )
@tarunkumar.d8379
@tarunkumar.d8379 Жыл бұрын
@@awwtawnoo I'll have to dry run , we save the space complexity in this solution so I gotta knw this one.
@awwtawnoo
@awwtawnoo Жыл бұрын
@@tarunkumar.d8379 Deffo, also my personal advice is follow Striver's LinkedList series along with this series, you'll get to know best approaches from both the playlists.
@tarunkumar.d8379
@tarunkumar.d8379 Жыл бұрын
@@awwtawnoo yes but he doesn't have merge k lists video, but hey I made a dry run an understood now how we get answer at 0 index, but how is it different from the first approach where we merger two lists then, merge tye resultant list with third list and so on?? And how TC is Log k?
@dailydestress6189
@dailydestress6189 Жыл бұрын
Bhai tum sab content creator sse request hai ki CRIO jaise company ko promote krna band kar do. They legit spam call me every week or so for registering in their program even when i am not interested.
@ShyamalDas-kd5vt
@ShyamalDas-kd5vt 2 жыл бұрын
Bade bade code mein Chhota Chhota error hote rehte Hain😅🤪
Remove Duplicates from Sorted List | EP 15
9:50
Fraz
Рет қаралды 17 М.
1❤️#thankyou #shorts
00:21
あみか部
Рет қаралды 88 МЛН
🍕Пиццерия FNAF в реальной жизни #shorts
00:41
Мы никогда не были так напуганы!
00:15
Аришнев
Рет қаралды 1,7 МЛН
Они убрались очень быстро!
00:40
Аришнев
Рет қаралды 3,6 МЛН
L25. Merge K Sorted Lists | Multiple Approaches
30:02
take U forward
Рет қаралды 26 М.
LRU cache | EP 22
19:51
Fraz
Рет қаралды 23 М.
Learn To Code Like a GENIUS and Not Waste Time
9:41
The Coding Sloth
Рет қаралды 1,2 МЛН
L23. Merge two sorted Linked Lists
18:55
take U forward
Рет қаралды 35 М.
1❤️#thankyou #shorts
00:21
あみか部
Рет қаралды 88 МЛН