← Home

88. Merge Sorted Array

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
88

The trick here is to name the state correctly, then let the implementation follow. For 88. Merge Sorted Array, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: straightforward implementation.

Guide

When?

This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are merge.

Guide

Why?

The solution works because it narrows the problem until every update has a clear reason to exist.

  • The code keeps the moving pieces local, so the main idea stays visible.

Guide

How?

Walk through the solution in this order:

  1. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 4 ms, faster than 100.00% of C++ online submissions for Merge Sorted Array.
02//Memory Usage: 8.6 MB, less than 100.00% of C++ online submissions for Merge Sorted Array.
03
04class Solution {
05public:
06    void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
07        int cur1 = 0, cur2 = 0;
08        nums1.resize(m);
09        nums2.resize(n);
10        while(cur2 < n){
11            if(m == 0 || nums1.back() <= nums2[cur2]){
12                //nums1 has been fully processed
13                nums1.push_back(nums2[cur2]);
14                //have processed a node in nums2
15                cur2++;
16            }else if(nums2[cur2] <= nums1[cur1]){
17                nums1.insert(nums1.begin()+cur1, nums2[cur2]);
18                //have processed a node in nums2
19                cur2++;
20            }
21            //every time we look at one node in nums1
22            cur1++;
23        }
24        nums1.resize(m+n);
25    }
26};
27
28//https://leetcode.com/problems/merge-sorted-array/discuss/29522/This-is-my-AC-code-may-help-you
29//Runtime: 4 ms, faster than 100.00% of C++ online submissions for Merge Sorted Array.
30//Memory Usage: 8.7 MB, less than 100.00% of C++ online submissions for Merge Sorted Array.
31
32class Solution {
33public:
34    void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
35        int i = m-1, j = n-1, k = m+n-1;
36        while(i >= 0 && j >= 0){
37            if(nums1[i] > nums2[j]){
38                nums1[k--] = nums1[i--];
39            }else{
40                nums1[k--] = nums2[j--];
41            }
42        }
43        
44        while(j >= 0){
45            nums1[k--] = nums2[j--];
46        }
47    }
48};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.