DEV Community

Cover image for Day 7 of Studying LeetCode Solution until I Can Solve One on My Own: Problem#88.Merge Sorted Array(Easy/JavaScript)
KillingLeetCode
KillingLeetCode

Posted on

Day 7 of Studying LeetCode Solution until I Can Solve One on My Own: Problem#88.Merge Sorted Array(Easy/JavaScript)

Intro: I am a former accountant turned software engineer graduated from coding bootcamp in January 2022. Algorithms and Data Structure is an unavoidable part of interviews for most of the tech companies now. And one of my friends told me that you need to solve a medium leetcode problem under 60 seconds in order to get into the top tech companies.So I thought I'd start learning how to do it while job searching.

Since I have no clue on how to solve any of the problems (even the easy ones), I thought there is no point for me to waste hours and can't get it figured out. Here is my approach:

  • Pick a leetcode problem randomly or Online Assessment from targeted companies.
  • Study 1-2 solutions from Youtube or LeetCode discussion section. One brute force solution, another one more optimal.
  • Write a blog post with detailed explanation and do a verbal walk through to help understand the solutions better.
  • Code out the solution in LeetCode without looking at the solutions
  • Combat the forgetting curve: Re-do the question for the next three days. And come back regularly to revisit the problem.

Problem#88. Merge Sorted Array

Difficulty: Easy Language: JavaScript

You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing the number of elements in nums1 and nums2 respectively.

Merge nums1 and nums2 into a single array sorted in non-decreasing order.

The final sorted array should not be returned by the function, but instead be stored inside the array nums1. To accommodate this, nums1 has a length of m + n, where the first m elements denote the elements that should be merged, and the last n elements are set to 0 and should be ignored. nums2 has a length of n.

Example 1:

Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
Output: [1,2,2,3,5,6]
Explanation: The arrays we are merging are [1,2,3] and [2,5,6].
The result of the merge is [1,2,2,3,5,6] with the underlined
elements coming from nums1.
Enter fullscreen mode Exit fullscreen mode

Example 2:

Input: nums1 = [1], m = 1, nums2 = [], n = 0
Output: [1]
Explanation: The arrays we are merging are [1] and [].
The result of the merge is [1].
Enter fullscreen mode Exit fullscreen mode

Example 3:

Input: nums1 = [0], m = 0, nums2 = [1], n = 1
Output: [1]
Explanation: The arrays we are merging are [] and [1].
The result of the merge is [1].
Note that because m = 0, there are no elements in nums1. The 0 is
only there to ensure the merge result can fit in nums1.
Enter fullscreen mode Exit fullscreen mode

Constraints:

  • nums1.length == m + n
  • nums2.length == n
  • 0 <= m, n <= 200
  • 1 <= m + n <= 200
  • -109 <= nums1[i], nums2[j] <= 109

Solution:

var merge = function(nums1, m, nums2, n) {

    for(i = 0; i < n; i++) {

//Loop (note 5) through array 'nums2' 

        nums1.splice(m+i,1,nums2[i])

//replace element in 'nums1' with element in 'nums2' one by one
//with splice (note 4)

    }

    nums1.sort(function(a,b){return a-b})

//sort (note 3) merged array 'nums1' in a non-decreasing order

};
Enter fullscreen mode Exit fullscreen mode

Solution Submission detail as of 2/16/2022
(Data below could vary since there are new submissions daily)

  • Runtime: 87 ms
  • Memory Usage: 42.1 MB

References:
LeetCode Problem Link
Note 2: for loop
Note 3: Array.sort()
Note 4: Array.splice()
Note 5: for loop
Blog Cover Image Credit

Top comments (0)