Link: https://leetcode.com/problems/merge-sorted-array/
Solution:
Topics: sorted order, in-place
Intuition
This is a very interesting problem if we aim for a constant space solution. Merging two sorted arrays is not very difficult because typically you would use two pointers and fill a new array with the values. This of course take o(n+m)
space. In this problem we are given nums1
with enough trailing zeros to store the merged list…and in fact the problem statement does imply that the algorithm should be constant space. So how do we merge nums1
with nums2
using only constant space?
I will admit that I struggled with this one and initially I came up with an o(n*m)
constant space solution. I thought this was ok because n
and m
are constrained to only 200
. But this did not sit with me well because my gut told me that there had to be a linear solution, and indeed there is.
The key here is to start filling up nums1
from the end. The reasoning here is that the zeros are at the end, so by starting there we avoid overwriting and/or complicated pointer logic. Typically, merging sorted arrays is done from front to back with the condition a < b
… but this logic can be flipped to a > b
if we choose to start from the back because the end of the merged list will be the largest value!
The implementation is a three pointer solution, and its still kind of tricky with many potential off-by-one errors.
The key takeaway from this problem is that when we modify arrays in-place, we should consider starting (and continuing) the algorithm in the empty spaces.
Implementation
Mnemonic
I don’t have a good one here…just think in-place-⇒start-where-empty
Review 1
This problem goes to show that my mnemonics and images don’t work. I remembered the trick without issue.