Merge k Sorted Lists - (Leetcode - 23) - (Google, Amazon, Microsoft..) : Explanation ➕ Live Coding

  Рет қаралды 10,536

codestorywithMIK

codestorywithMIK

Күн бұрын

Пікірлер: 63
@JJ-tp2dd
@JJ-tp2dd Жыл бұрын
Unbelievable! How are you so so good bhai? Java Implementation: class Solution { private ListNode mergeTwoSortedList(ListNode l1, ListNode l2) { if(l1 == null) return l2; if(l2 == null) return l1; if(l1.val l2.val) { l2.next = mergeTwoSortedList(l1,l2.next); return l2; } return null; } private ListNode paritionAndMerge(int s, int e, ListNode[] lists) { if(s > e) { return null; } if(s == e) { return lists[s]; } int mid = s +(e-s)/2; ListNode L1 = paritionAndMerge(s,mid,lists); ListNode L2 = paritionAndMerge(mid+1,e,lists); return mergeTwoSortedList(L1,L2); } public ListNode mergeKLists(ListNode[] lists) { int k = lists.length; if(k == 0) { return null; } return paritionAndMerge(0,k-1,lists); } }
@GghiggFfhhhf
@GghiggFfhhhf 5 күн бұрын
15 min ke baad khud se, code likh ke submit and got accepted the way he explain 🙏🙏😻😻
@itsaayush7357
@itsaayush7357 3 ай бұрын
Clear, concise and simple explanation!! India has some unique talents. When it comes to code so we have best of best teachers available. In every domain there is one king, and in DSA we have our mik.
@guptajiivlogging8174
@guptajiivlogging8174 Жыл бұрын
Please bhaiya, it's my humble request to you please don't stop teaching dsa and cp and posting videos on solving question.. It help's a lot. After 5-6 months companies are going to visit our college so you and your channel is going to help me and my friends alot....Lots of love from jabalpur (MP)
@souravjoshi2293
@souravjoshi2293 Жыл бұрын
Same bro
@nawazthezaifre8870
@nawazthezaifre8870 9 ай бұрын
placement hoa kya bhai ??
@22gauravkumar70
@22gauravkumar70 5 ай бұрын
@@nawazthezaifre8870 placement hua?
@shweta1013
@shweta1013 7 ай бұрын
You are just awesome !!.. Cant believe i wrote the code myself after listening to this video !!
@Ramanujan-c5c
@Ramanujan-c5c 2 ай бұрын
Beautiful code .... creative way to solve it. Loved it. I heard that u are quite good coder, its true. U deserve the fame as striver... launch few short videos here and on linkedin. Hope u reach to great heights. I am really thankful to u for sharing so many great codes and solving problems with so ease. Love ur solution. Thank u so much for helping newbie coders and turning them towards Guardian of Ltc and red coder of CFr. Keep shining Sir. Tc.
@codestorywithMIK
@codestorywithMIK 2 ай бұрын
This means a lot. Thank you ❤️❤️❤️
@nik3889
@nik3889 3 ай бұрын
how ! i never seen that type of solution that is the best solution i ever watched
@souravjoshi2293
@souravjoshi2293 Жыл бұрын
I think you are the best who can make things easier like a cake walk
@akshaychavan5511
@akshaychavan5511 6 ай бұрын
This question felt hard for me when I initially attempted it. But, now it feels quite easy.
@atifhu
@atifhu Жыл бұрын
Bhai kya smjhate ho❤️❤️
@gui-codes
@gui-codes 6 ай бұрын
too good man. becoming your fan day by day. One stop for revision
@vineetkumar2899
@vineetkumar2899 Жыл бұрын
awesome bhaiya😍
@mayankagarwal6775
@mayankagarwal6775 2 ай бұрын
really really good explanation. thanks for your efforts😃
@ShivaaySingh20
@ShivaaySingh20 7 ай бұрын
Thank you so much bhai for making this question very easy to understand 🙌❤.
@tutuimam3381
@tutuimam3381 Жыл бұрын
Amazing explanation🌹
@elakstein
@elakstein Жыл бұрын
Good one. Just want to point out that currently the time complexity is O(NlogK) and space complexity is O(logK) because of recursion stack, it can be solved iteratively with the same time complexity but with O(1) space complexity.
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Indeed
@alphadrones23
@alphadrones23 Жыл бұрын
yup.. we could use the iterative approach to merge the two lists
@zgodgaming857
@zgodgaming857 9 ай бұрын
the best explanation
@khushijain3574
@khushijain3574 Жыл бұрын
you explained so nicely keep making such videos
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you so much 😇🙏
@khushijain3574
@khushijain3574 Жыл бұрын
@@codestorywithMIK please create video on quick sort on linked list , i want your approach to solve this thanks
@NamanSaxenaa
@NamanSaxenaa 16 күн бұрын
GOD level! 🔥
@shabananoor9423
@shabananoor9423 Жыл бұрын
Best explanation
@abhinay.k
@abhinay.k 4 ай бұрын
great video
@Methre_
@Methre_ 10 ай бұрын
very nice bhai;
@nihalsingh6233
@nihalsingh6233 Жыл бұрын
Thank You soo much sir !! You really made it easy
@codestorywithMIK
@codestorywithMIK Жыл бұрын
I am so glad it helped ❤️❤️❤️
@JJ-tp2dd
@JJ-tp2dd Жыл бұрын
Merge two sorted List -- Leetcode 21 class Solution { private ListNode mergeTwoSortedList(ListNode l1, ListNode l2) { if(l1 == null) return l2; if(l2 == null) return l1; if(l1.val l2.val) { l2.next = mergeTwoSortedList(l1,l2.next); return l2; } return null; } public ListNode mergeTwoLists(ListNode list1, ListNode list2) { return mergeTwoSortedList(list1, list2); } }
@mudassarnazar1663
@mudassarnazar1663 Жыл бұрын
You are genius ,
@U2011-n7w
@U2011-n7w Жыл бұрын
very good explanation pls make video on today leetcode contest questions
@codestorywithMIK
@codestorywithMIK Жыл бұрын
I will soon start those as well . Just trying to make plans as per my time management. Soon
@priyanshudubey2245
@priyanshudubey2245 Жыл бұрын
Bhaiya weekly contests ke questions par bhi video bana dijiye please.
@codestorywithMIK
@codestorywithMIK Жыл бұрын
I will soon start those as well . Just trying to make plans as per my time management. Soon
@RohitYadav-uo6uo
@RohitYadav-uo6uo Жыл бұрын
can't we use a for loop in which we merge first two then we will merge the answer of that to next list and like that isn't that a good way?
@saisathvikreddyloka7578
@saisathvikreddyloka7578 Жыл бұрын
🔥🔥
@ishwarkokkili7646
@ishwarkokkili7646 Жыл бұрын
Masterful !!
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you so much 🙏❤️😇
@shivamjadon2257
@shivamjadon2257 Жыл бұрын
mast video
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you 😇🙏
@sunshineandrainbow5453
@sunshineandrainbow5453 4 ай бұрын
What is the need of partition and merge, we can simply use a for loop from i =2 to n-1 and merge the result of lists[0] & lists[1] with lists[i] . At each iteration mergedList = merge(lists[i],mergedList) ...
@mindpower185
@mindpower185 4 ай бұрын
@codestorywithMIK ek baar just Merge K sorted array wala ...explain kardo ya github pe upload kardo please
@harshlakra3260
@harshlakra3260 9 ай бұрын
concepts ki video kab aarhi h
@surajsidar3280
@surajsidar3280 5 ай бұрын
If I submit Python code then There is TLE error if we submit using an iterative approach There is a maximum recursive depth error if I follow the recursive approach.
@vishalgautam-f6v
@vishalgautam-f6v Жыл бұрын
what will be the recurrence relation for the given algorithm? If it is T(k) = 2*T(k/2)+n then it will result in n*k or if it is not the correct recurrence relation then what will be the correct relation?
@ShivaaySingh20
@ShivaaySingh20 7 ай бұрын
interview me yadi questions puche to mai bhi interviewer se kahunga "Aao Story Se Code Kre "😁😅
@codestorywithMIK
@codestorywithMIK 7 ай бұрын
Cool 😅❤️
@nik3889
@nik3889 3 ай бұрын
bhai ek bar k sorted array wala kra do
@MakeuPerfect
@MakeuPerfect Жыл бұрын
bhaiya minimum height trees wala question karao plz
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Can you please share leetocde link
@MakeuPerfect
@MakeuPerfect Жыл бұрын
@@codestorywithMIK meine link diya tha bhej diya but ye dikhai nahi de raha comment mein
@AshishSharma-tf3fy
@AshishSharma-tf3fy 8 ай бұрын
just using vector property to skip binary search algo class Solution { public: ListNode* mergetwo(ListNode* l1,ListNode* l2){ if(l1==NULL) return l2; if(l2==NULL) return l1; if(l1->valval){ l1->next=mergetwo(l1->next,l2); return l1; } else{ l2->next=mergetwo(l1,l2->next); return l2; } } ListNode* mergeKLists(vector& lists) { if(lists.size()==0) return NULL; while(lists.size()>1){ ListNode* new_node=mergetwo(lists[0],lists[1]); lists.push_back(new_node); lists.erase(lists.begin()); lists.erase(lists.begin()); } return lists[0]; } };
@drbullah1388
@drbullah1388 Жыл бұрын
Bhaiya, i thought of this on the first go ListNode* mergeTwoLists(ListNode* list1, ListNode* list2){ ListNode* head = NULL; ListNode* tail = NULL; if(list1==NULL) return list2; if(list2==NULL) return list1; if(list1->val < list2->val){ head = list1; list1 = list1->next; } else{ head = list2; list2 = list2->next; } tail = head; while(list1 && list2){ if(list1->val < list2->val){ tail->next = list1; list1 = list1->next; } else{ tail->next = list2; list2=list2->next; } tail=tail->next; } if(!list1) tail->next=list2; else tail->next=list1; return head; } ListNode* mergeKLists(vector& lists) { int k = lists.size(); if(k == 0){ return NULL; } if(k == 1){ return lists[0]; } ListNode* res = mergeTwoLists(lists[0], lists[1]); for(int i = 2 ; i < k ; i++){ res = mergeTwoLists(res, lists[i]); } return res; } Ye bhi sahi hai na?
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Yes it should work too.
@harsh.jain22
@harsh.jain22 Жыл бұрын
I also thought of the exact same solution of iteration in first go 🤜🤛
@yashsharma1774
@yashsharma1774 10 ай бұрын
Your time complexity calculation is incorrect. It will be O(k*n). This is because you have perform calculation at each leaf here but in case of binary search you do calculation at each node of traversed path.
@Whirlwind03
@Whirlwind03 Жыл бұрын
Beautiful solution brother, I solved it using PriorityQueue now will solve using your approach public class Pair implements Comparable { ListNode node; public Pair(ListNode node){ this.node = node; } @Override public int compareTo(Pair o){ return this.node.val - o.node.val; } } public ListNode mergeKLists(ListNode[] lists) { if(lists.length == 0) return null; ListNode res = new ListNode(-1); ListNode t = res; PriorityQueue pq = new PriorityQueue(); for(int i=0;i 0){ Pair rem = pq.remove(); t.next = rem.node; t = t.next; if(rem.node.next != null){ pq.add(new Pair(rem.node.next)); } } return res.next; }
Twin Telepathy Challenge!
00:23
Stokes Twins
Рет қаралды 118 МЛН
Creative Justice at the Checkout: Bananas and Eggs Showdown #shorts
00:18
Fabiosa Best Lifehacks
Рет қаралды 6 МЛН
Smart Sigma Kid #funny #sigma
00:33
CRAZY GREAPA
Рет қаралды 7 МЛН
Merge K Sorted Lists - Leetcode 23 - Python
10:34
NeetCode
Рет қаралды 207 М.
L25. Merge K Sorted Lists | Multiple Approaches
30:02
take U forward
Рет қаралды 52 М.
How I Mastered Data Structures and Algorithms in 8 Weeks
15:46
Aman Manazir
Рет қаралды 102 М.
Why is Python 150X slower than C?
10:45
Mehul - Codedamn
Рет қаралды 17 М.