Version change of

Posted by oneday on Fri, 07 Jan 2022 10:58:04 +0100

Title Description

You are given two integer arrays nums1 and nums2 in non decreasing order, and two integers m and n representing the number of elements in nums1 and nums2 respectively. Please merge nums2 into nums1 so that the merged array is also arranged in non decreasing order.

Title Example

• 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: consolidation is required [1,2,3] and [2,5,6] .
The combined result is [1,2,2,3,5,6] ，Where bold italics are nums1 Elements in.
• Example 2:
Input: nums1 = , m = 1, nums2 = [], n = 0
Output:
Explanation: consolidation is required  and [] .
The combined result is  .
• Example 3:
Input: nums1 = , m = 0, nums2 = , n = 1
Output:
Explanation: the array to be merged is [] and  .
The combined result is  .
Attention, because m = 0 ，therefore nums1 There are no elements in the. nums1 The only remaining 0 is to ensure that the merged results can be successfully stored in the nums1 Yes.
• Tips:
nums1.length == m + n
nums2.length == n
0 <= m, n <= 200
1 <= m + n <= 200
-10^9 <= nums1[i], nums2[j] <= 10^9

Java method: sort after direct merge

Ideas and algorithms

The most intuitive method is to put array nums2 into the tail of array nums1, and then sort the whole array directly.

code

class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
for (int i = 0; i != n; ++i) {
nums1[m + i] = nums2[i];
}
Arrays.sort(nums1);
}
}

results of enforcement

• Execution result: passed
• Execution time: 1 ms, beating 17.63% of users in all Java submissions
• Memory consumption: 38.3 MB, beating 81.72% of users in all Java submissions

Complexity

• Time complexity: O((m+n)log(m+n))
• Spatial complexity: O(log(m+n))

Java method: double pointer

Ideas and algorithms

You can use the double pointer method. In this method, two arrays are regarded as queues, and small numbers are taken from the heads of two arrays each time and put into the results. Set a pointer p1 and p2 for the two arrays as the head pointer of the queue.

code

class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
int p1 = 0, p2 = 0;
int[] sorted = new int[m + n];
int cur;
while (p1 < m || p2 < n) {
if (p1 == m) {
cur = nums2[p2++];
} else if (p2 == n) {
cur = nums1[p1++];
} else if (nums1[p1] < nums2[p2]) {
cur = nums1[p1++];
} else {
cur = nums2[p2++];
}
sorted[p1 + p2 - 1] = cur;
}
for (int i = 0; i != m + n; ++i) {
nums1[i] = sorted[i];
}
}
}

results of enforcement

• Execution result: passed
• Execution time: 0 ms, beating 100.00% of users in all Java submissions
• Memory consumption: 38.5 MB, beating 58.52% of users in all Java submissions

Complexity

• Time complexity: O(m+n)
• Space complexity: O(m+n)

Java method: reverse double pointer

Ideas and algorithms

The second half of nums1 is empty and can be directly overwritten without affecting the results. Therefore, the pointer can be set to traverse from back to front, taking the larger of the two each time and putting it at the end of nums1. Strictly speaking, at any time during the traversal, m − p1 − 1 elements in the nums1 array are placed in the latter half of nums1, n − p2 − 1 elements in the nums2 array are placed in the latter half of nums1, and after the pointer p1, the nums1 array has m+n − p1 − 1 positions. Because m+n − p1
− 1 ≥ m − p1 − 1+n − p2 − 1 is equivalent to that p2 ≥ − 1 always holds. Therefore, the position behind p1 is always enough to accommodate the inserted elements, and the elements of p1 will not be covered.

code

class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
int p1 = m - 1, p2 = n - 1;
int tail = m + n - 1;
int cur;
while (p1 >= 0 || p2 >= 0) {
if (p1 == -1) {
cur = nums2[p2--];
} else if (p2 == -1) {
cur = nums1[p1--];
} else if (nums1[p1] > nums2[p2]) {
cur = nums1[p1--];
} else {
cur = nums2[p2--];
}
nums1[tail--] = cur;
}
}
}

results of enforcement

• Execution result: passed
• Execution time: 0 ms, beating 100.00% of users in all Java submissions
• Memory consumption: 38.6 MB, beating 31.81% of users in all Java submissions

Complexity

• Time complexity: O(m+n)
• Space complexity: O(1)