You get two integer arrays, nums1 and nums2, each sorted from small to large, plus two counts: m and n. Only the first m slots of nums1 hold real values. Its last n slots are filler zeros, there to make room. nums2 holds exactly n values.
Combine the two into one sorted sequence, written into `nums1` itself. Return nothing: the caller reads nums1 after your function ends.
Example 1
Input: nums1 = [2, 4, 7, 0, 0, 0], m = 3, nums2 = [1, 5, 8], n = 3 Output: [1, 2, 4, 5, 7, 8] Why: the real values 2, 4, 7 and 1, 5, 8 interleave
2 more examples and the constraints
Example 2
Input: nums1 = [5, 6, 0, 0], m = 2, nums2 = [1, 2], n = 2 Output: [1, 2, 5, 6] Why: every value of nums2 is smaller, so it all goes first
Example 3
Input: nums1 = [0], m = 0, nums2 = [4], n = 1 Output: [4] Why: m = 0, so the 0 is only room, not a value
Constraints
nums1has lengthm + n, andnums2has lengthn- 0 ≤ m, n ≤ 200, and 1 ≤ m + n ≤ 200
- −1,000,000,000 ≤ each value ≤ 1,000,000,000
- Both inputs are already sorted in non-decreasing order
Your turn first
Have a go on paper or in your own editor, then reveal the approaches from the simplest up.
- Copy, then sortO(k log k)
- Merge from the front, with a copyO(m + n)
- Merge from the backO(m + n)
The idea in one sentence
The pattern, how to spot it, and the best complexity.
Side by side
| Approach | Time | Relative work for m + n = 200 | Verdict |
|---|---|---|---|
| Copy, then sort | O(k log k) | Simple; ignores that the inputs are sorted | |
| Merge from the front | O(m + n) | Linear, but needs a copy of nums1 | |
| Merge from the back | O(m + n) | Best; linear and in place |
m and n = number of real values in nums1 and nums2, k = m + n.
Edge cases to test
- nums2 is empty. n = 0:
nums1is already the answer, and the loop never runs. - nums1 has no real values. m = 0:
istarts at −1, so every write comes fromnums2. - Everything in nums2 is smaller. All of nums1's values shift to the back first, then nums2 fills the front.
- Equal values. Ties can go either way; the result is the same sorted list.
- Real zeros. A 0 inside the first
mslots is a value, not filler. Trustm, not the zeros.
Common mistakes
- Looping
while i >= 0 and j >= 0and forgetting to copy what is left ofnums2afterwards. - Reading
nums1[i]wheniis already −1, which wraps to the last element in Python. - Merging from the front without a copy, so values of
nums1are overwritten before they are read.
How to talk through it
- 1Clarify. Is the filler always at the end of nums1? Can m or n be 0? Do I return anything?
- 2Start simple. Copy nums2 into the tail and sort: correct, but O(k log k).
- 3Use the order. Both inputs are sorted, so a two-pointer merge is O(m + n). From the front it needs a copy.
- 4Flip the direction. Fill from the back: the free slots are there, so no copy is needed and nothing is overwritten early.
- 5Test out loud. Try n = 0, m = 0, and a case where all of nums2 is smaller.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?