Merge Sorted Array | LeetCode 88 | Coding Interview Tutorial
Vložit
- čas přidán 26. 07. 2024
- Merge Sorted Array solution: LeetCode 88
Code and written explanation: terriblewhiteboard.com/merge-...
Link to problem on LeetCode: leetcode.com/problems/merge-s...
Buy Me a Coffee: www.buymeacoffee.com/terrible...
AFFILIATE LINKS
If you're interested in learning algorithms, these are great resources.
💻 40% off Tech Interview Pro: techinterviewpro.com/terriblew...
⌨️ 20% off CoderPro: coderpro.com/terriblewhiteboard
💲 All coupons and discounts 💲
terriblewhiteboard.com/coupon...
Merge Sorted Array | LeetCode 88 | Coding Interview
#mergesortedarray #leetcode #algorithms #terriblewhiteboard #codinginterview
Click the time stamp to jump to different parts of the video.
00:00 Title
00:06 Problem readout
00:56 Whiteboard solution
03:40 Coding solution
10:40 Result and outro
This solution is valid for JavaScript. You may have to modify it to fit your particular language.
Java solution:
class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
int first = m - 1;
int second = n - 1;
for (int i = nums1.length - 1; i >= 0; i--) {
if (second < 0) {
return;
}
if (first >= 0 && nums1[first] > nums2[second]) {
nums1[i] = nums1[first];
first--;
} else {
nums1[i] = nums2[second];
second--;
}
}
}
}
I got stuck at the test case [0], 0, [1],1, I missed the first >= 0 condition. Thanks for your java solution. - :)
thanks for your solution
such a beautiful solution
Thanks!
I totally agree
It's a very good solution, thank you so much!
Thanks!
need to add:
if(first < 0){
nums1[i] = nums2[second];
second--;
}
to account for new test cases
Actually you need to add this
if first < 0:
while second >= 0:
nums1[replaceIndex] = nums2[second]
second-=1
replaceIndex-=1
wow thanks
You're welcome!
good work
can you put the python version of this pls?
I would, but I don't have time to make videos in more than one language. I'm hoping the whiteboarding can be translated to multiple languages, though.
leetcode.com/problems/merge-sorted-array/discuss/29522/This-is-my-AC-code-may-help-you
Any idea why the solution fails if I switch the order of if conditions as: if (nums1[first]
Why start from the end? And what is the difference if starting from front?
Hello, I am wondering this as well. Please advise. Thanks a lot!
Hi, I'm revisiting this to see why start from the end and whats the difference....Thanks a lot for your contribution through creating this page
It fails for this test-case:
Input:
[2,0]
1
[1]
1
Output:
[2,2]
Expected:
[1,2]
leetcode.com/problems/merge-sorted-array/discuss/29522/This-is-my-AC-code-may-help-you
add if(first < 0){
nums1[i] = nums2[second];
second--;
}
to top of if/else chain
first = m - 1
second = n - 1
for i in range (m+n-1,-1,-1):
if second < 0 :
break;
elif first < 0:
nums1[i] = nums2[second]
second -= 1
elif nums1[first] > nums2[second]:
nums1[i] = nums1[first]
first -= 1
else:
nums1[i] = nums2[second]
second -= 1
This solution is only valid if m>n.
it will not work if m
It works for javascript. You might have to make minor adjustments if you're using another language.
It's literally given in the question to merge nums2 into nums1
A note from the question - You may assume that nums1 has enough space (size that is equal to m + n) to hold additional elements from nums2.
I can't pass one test case i.e:
[0]
0
[1]
1
since, first = m-1, and m is 0 in this case, so first becomes negative and it gives a runtime error
hope you see this
same here! any solution u found ?
Solution covering edge cases mentioned here:
var merge = function(nums1, m, nums2, n) {
let firstLastElementIndex = m -1;
let secondLastElementIndex = n -1;
for (let x=(m + n -1); x >= 0; x--) {
if (secondLastElementIndex < 0) {
break;
}
if (nums1[firstLastElementIndex] < nums2[secondLastElementIndex] ||
firstLastElementIndex < 0 ||
m === 0) {
nums1[x] = nums2[secondLastElementIndex];
secondLastElementIndex--;
} else {
nums1[x] = nums1[firstLastElementIndex];
firstLastElementIndex--;
}
}
};
you missed a condition..what if first
Why is there no return statement to show the output?
Because you're supposed to modify nums1, you don't need to return anything to do that
I am dump that's it