Merge Sorted Array - LeetCode 88 - Java

preview_player
Показать описание
After watching this video, you will not need to watch anything else regarding merging 2 sorted arrays. Best of luck and I hope you enjoy the video!

Video contents:

00:00 - Read and understand the question
01:44 - Explain the solution on the black board
08:05 - Code the solution
10:57 - Time and Space complexity
Рекомендации по теме
Комментарии
Автор

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

observer
Автор

You just got yourself a new follower. What a beautiful beautiful explanation, wow

athenkosimamfengu
Автор

Since i was searching this question for a better approach. thank you for this explanation.

shashankvashishtha
Автор

this is what i expect from my teacher anyways you teach really good bro😀

RohanKumar-nxry
Автор

This is really nice. A refreshing approach and good explanations. Subscribed

notjohnAtall
Автор

Nice approach ! Thank you for sharing.

melanieprevot
Автор

Thank yo so much for this amazing explanation. Really really good!

suggestaname
Автор

very good. I am now a subscriber. great work.

chadsmith
Автор

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--;
}
}
}
}

musamehdiyevv
Автор

I did not understand why use the second while loop?

swapnil
Автор

are u going to do all leetcode questions

Jaffar
Автор

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

alastairhewitt
join shbcf.ru