Thank you for the explanation. I think this code is simpler: class Solution { public void merge(int[] nums1, int m, int[] nums2, int n) { int p = m + n - 1; int p1 = m - 1; int p2 = n - 1; while (p2 >= 0) { if (p1 >= 0 && nums1[p1] > nums2[p2]) { nums1[p] = nums1[p1]; p1--; p--; } else { nums1[p] = nums2[p2]; p2--; p--; } } } }
Fantastic explanation Edit: In the while loop, if I only write p1 >= 0, then I get an index out of bound error, but if I include the logic for p1 & p2 then the code up until that point works properly. I am not really sure why that is since m & n are both 3. Can someone please help explain what I am not seeing? To me it is the same thing
one of the rare videos where the instructor not only is good at solving but also, and more importantly in this case, explaining the solution
I really appreciate your kind words!
@@LeetCodeUniversity i wish you could do all 150 top interview questions ;)
@@observer861I have to get back to it for sure!
@@LeetCodeUniversity +1 looking for top 15o interview questions
You just got yourself a new follower. What a beautiful beautiful explanation, wow
I really appreciate that!!
Since i was searching this question for a better approach. thank you for this explanation.
Thank you very much my friend!
This is really nice. A refreshing approach and good explanations. Subscribed
Thank you for the kind words!
Nice approach ! Thank you for sharing.
You are very welcome!
this is what i expect from my teacher anyways you teach really good bro😀
Thank you! I absolutely love to teach, glad you found it helpful!
Thank yo so much for this amazing explanation. Really really good!
Thank you so much, I am so glad You benefited from the video!
very good. I am now a subscriber. great work.
Appreciate that!
Thanks for the video.
My pleasure!
Thank you for the great explanation!
Diana, you are very welcome!
Love your explanation!
Thank you, I am so glad I helped!
Great Explanation. Thank you so much.
Great explanation..
Thank you!
thank you 😊😊😊😊
You are very welcome!
Thanks
I did not understand why use the second while loop?
please upload more questions
I will!
are u going to do all leetcode questions
The plan is to do Blind 75 list and then do pattern based questions
Thanks it would be nice if you do all the leetcode questions
Thank you for the explanation. I think this code is simpler:
class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
int p = m + n - 1;
int p1 = m - 1;
int p2 = n - 1;
while (p2 >= 0) {
if (p1 >= 0 && nums1[p1] > nums2[p2]) {
nums1[p] = nums1[p1];
p1--;
p--;
} else {
nums1[p] = nums2[p2];
p2--;
p--;
}
}
}
}
will this work for every array
For sure!
Nice
Fantastic explanation
Edit:
In the while loop, if I only write p1 >= 0, then I get an index out of bound error, but if I include the logic for p1 & p2 then the code up until that point works properly.
I am not really sure why that is since m & n are both 3. Can someone please help explain what I am not seeing? To me it is the same thing