0

I'm trying to solve this question on LeetCode:

Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.

Note: The number of elements initialized in nums1 and nums2 are m and n respectively.

You may assume that nums1 has enough space (size that is equal to m + n) to hold additional elements from nums2.


I ended up with coming up with this code:

var merge = function(nums1, m, nums2, n) {
  var nums = [];
  nums1.length = m;
  nums2.length = n;
  nums = nums.concat(nums1);
  console.log(nums);
  nums = nums.concat(nums2);
  console.log(nums);
  nums = nums.sort();
  console.log(nums);
  return nums;
}

This is what the 'run code' says:

Your input

[1,2,3,0,0,0]

3

[2,5,6]

3

stdout

[ 1, 2, 3 ]

[ 1, 2, 3, 2, 5, 6 ]

[ 1, 2, 2, 3, 5, 6 ]

Output:

[1,2,3]

Expected

[1,2,2,3,5,6]

(an image version if the quotes weren't clear)

an image version if the quotes weren't clear

When I'm console.logging the array, the answer is correct but for some reason returning the array gives a completely different output

Can anyone help in this?

5
  • 1
    You return a new array nums. You should probably be returning nums1. Commented Nov 23, 2020 at 6:56
  • if console.log(nums) is giving the expected output but returned value of nums is different without changing anything, then you must be returning num1 instead of nums XD Commented Nov 23, 2020 at 6:58
  • @VLAZ but I'm concatenating the two arrays to nums, so returning nums1 would just return an array without the second array that I have to concatenate. Commented Nov 23, 2020 at 7:03
  • I'll try concatenating in nums1 instead Commented Nov 23, 2020 at 7:04
  • 2
    @KAI Sorry, I didn't express myself clearly - you should be working with nums1 - the test seems to only be checking that, rather than the return value. When you make a new array, you're not changing nums1 Commented Nov 23, 2020 at 7:04

1 Answer 1

3

I think the hint here is that nums1 is big enough to hold n+m values. So the way to solve this problem is to work backwards through both arrays, filling the empty space in nums1 as you go. So for example, for the first iteration of the loop, you would compare nums1[n-1] and nums2[m-1] and put whichever is larger into nums1[n+m-1]. You then continue this for as long as you have values in either array, copying exclusively from the other if one runs out:

const merge = function(nums1, m, nums2, n) {
  n--; m--;
  for (let i = m + n + 1; i >= 0; i--) {
    nums1[i] = (m < 0 || n >= 0 && nums2[n] > nums1[m]) ? nums2[n--] : nums1[m--];
  }
  return nums1;
}

console.log(merge([1, 2, 3, 0, 0, 0], 3, [2, 5, 6], 3));

This code is O(n) (as compared to your sort which is O(nlogn)) and requires no additional space.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much. This makes complete sense and it's really useful

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.