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.
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.
The arrays we are merging are [1] and [].
The result of the merge is [1].
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.
nums1.length == m + nnums2.length == n0 <= m, n <= 2001 <= m + n <= 200-10^9 <= nums1[i], nums2[j] <= 10^9Follow up: Can you come up with an algorithm that runs in O(m + n) time?
No developer has claimed the Python throne for LeetCode #88 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
sort() method of a programming language to sort the array, which is very simple to implement. However, the time complexity of this approach is O(n log n).O(n). How can we achieve O(n)?nums1 is not the smallest, it can’t be directly replaced and needs to be stored somewhere temporarily. A simple workaround is to create a new empty array to hold the final result, and then copy that array back into nums1. But this increases the amount of code you need to write.class Solution:
def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
"""
Do not return anything, modify nums1 in-place instead.
"""
# Make a good example first.
# nums1: [1, 5, 7, 0, 0, 0, 0]
# nums2: [2, 3, 4, 8]
# result:[1, 2, 3, 4, 5, 7, 8]
i = m - 1
j = n - 1
for k in range(len(nums1) - 1, -1, -1):
if i < 0 or (j >= 0 and nums2[j] > nums1[i]):
nums1[k] = nums2[j]
j -= 1
else:
nums1[k] = nums1[i]
i -= 1
# Solution two.
class Solution:
def merge(self, nums1: list[int], m: int, nums2: list[int], n: int) -> None:
nums = [] # use a new array to save ordered num.
i = 0
j = 0
while i < m or j < n:
if j >= n or (i < m and nums1[i] <= nums2[j]):
nums.append(nums1[i])
i += 1
else:
nums.append(nums2[j])
j += 1
for i, num in enumerate(nums):
nums1[i] = num
No developer has claimed the Ruby throne for LeetCode #88 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
sort() method of a programming language to sort the array, which is very simple to implement. However, the time complexity of this approach is O(n log n).O(n). How can we achieve O(n)?nums1 is not the smallest, it can’t be directly replaced and needs to be stored somewhere temporarily. A simple workaround is to create a new empty array to hold the final result, and then copy that array back into nums1. But this increases the amount of code you need to write.# @param {Integer[]} nums1
# @param {Integer} m
# @param {Integer[]} nums2
# @param {Integer} n
# @return {Void} Do not return anything, modify nums1 in-place instead.
def merge(nums1, m, nums2, n)
# Make a good example first.
# nums1: [1, 5, 7, 0, 0, 0, 0]
# nums2: [2, 3, 4, 8]
# result:[1, 2, 3, 4, 5, 7, 8]
i = m - 1
j = n - 1
(nums1.size - 1).downto(0) do |k|
if i < 0 || (j >= 0 and nums2[j] > nums1[i])
nums1[k] = nums2[j]
j -= 1
else
nums1[k] = nums1[i]
i -= 1
end
end
end
No developer has claimed the Java throne for LeetCode #88 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
sort() method of a programming language to sort the array, which is very simple to implement. However, the time complexity of this approach is O(n log n).O(n). How can we achieve O(n)?nums1 is not the smallest, it can’t be directly replaced and needs to be stored somewhere temporarily. A simple workaround is to create a new empty array to hold the final result, and then copy that array back into nums1. But this increases the amount of code you need to write.class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
// Make a good example first.
// nums1: [1, 5, 7, 0, 0, 0, 0]
// nums2: [2, 3, 4, 8]
// result:[1, 2, 3, 4, 5, 7, 8]
var i = m - 1;
var j = n - 1;
for (var k = nums1.length - 1; k >= 0; k--) {
if (i < 0 || (j >= 0 && nums2[j] > nums1[i])) {
nums1[k] = nums2[j];
j--;
} else {
nums1[k] = nums1[i];
i--;
}
}
}
}
No developer has claimed the C++ throne for LeetCode #88 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Javascript throne for LeetCode #88 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Go throne for LeetCode #88 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Rust throne for LeetCode #88 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the C# throne for LeetCode #88 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Swift throne for LeetCode #88 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Kotlin throne for LeetCode #88 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.