Merge Sorted Array - Leetcode 88 - Python

preview_player
Показать описание


0:00 - Drawing explanation
5:30 - Coding solution

Leetcode 88
#Coding #leetcode #neetcode

Disclosure: Some of the links above may be affiliate links, from which I may earn a small commission.
Рекомендации по теме
Комментарии
Автор

yeah ngl this wasn't "easy" imo. this really helped me though, thanks

theoreticalphysics
Автор

Great solution man!

Managed to come up with a slightly cleaner solution that avoids having to loop over n at the end to continue filling:

i = m+n-1, m = m-1, n = n-1

while n >= 0:
if m >= 0 and nums1[m] > nums2[n]:
nums1[i] = nums1[m]
m-=1
else:
nums1[i] = nums2[n]
n-=1
i-=1

Because we only care about iterating n overall, as that's our indicator that the fill has been completed, we can remove m>=0 from our while loop conditions and just ensure that we no longer consider m in our fill if it's completed.

Both solutions ar efine, but if it's concision you're looking for, I hope that helps guys!

jordantran
Автор

Clear and Crisp explanation, always love your explanations . Gracias !

aritralahiri
Автор

Not as marquee as other medium or hard problems are but I do have some takeaways from it. Thanks for your video as always 🥰

harpercfc_
Автор

its certainly not a leetcode easy its medium difficulty.

Shubhamkumar-ngpm
Автор

A highly underrated channel. Love this one

rustwithoutrust
Автор

I really appreciate that example and clarity on that edge case.

jacksparrw
Автор

Thank you, with your step by step explanation, it was easy to grasp and follow.

sarvarkhalimov
Автор

can anyone explain me last line of code? would not while loop also work for the leftovers too? just like 3, 2 swapping position ?

azijulmunsi
Автор

Thanks for the video! Exactly at 3:13, when you said "How do we get the largest value?", I already got the idea how to optimally solve the problem.

TheVzlalover
Автор

thanks! Finally solved after 1 day of struggling :(

qbmain
Автор

Very clear and helpful explanation, thanks!

Ozaki
Автор

Nice and clear explanation at the begining. Thanks.

hernanvelazquez
Автор

This was a superb explaination! Other videos were complicating it.

siyandasphesihlengcobo
Автор

which section is this under on the site? i'm not seeing it

shleebeez
Автор

Thank you for this video! Yet, I think line 14 is not necessary, and line 19 can be changed to n-=1. The reason is value "last" will decrease automatically if m or n decreases by 1. I tried it, and it worked!

andrewchen
Автор

Excellent explanation!
key example i feel for this problem is nums1 = [4, 5, 6] and nums2=[1, 2]; also using write_pointer instead of last was super helpful while writing code

jsarvesh
Автор

Yay, I managed to get this one one my own. I was wondering what other algorithm you could've done, since an O(m+n) solution is supposed to be optimal.

spaghettiking
Автор

We already checked n > 0 in the first while loop. Why we need secondary while loop with n > 0 ?

Jakirseu
Автор

Why the condition for while loop is > 0, it should be >=0 right? Because it needs to chsck the first element also

d_starcode