class Solution:
def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
"""
Merges two sorted arrays (nums1 and nums2) into one sorted array in-place.
Parameters:
nums1 (List[int]): The first sorted array, which has a length of m + n.
The first m elements are valid, followed by n zeros to provide extra space.
m (int): Number of valid elements in nums1.
nums2 (List[int]): The second sorted array containing n valid elements.
n (int): Number of elements in nums2.
Operation:
The function modifies nums1 in-place, merging it with nums2 such that the final array is sorted
in non-decreasing order. The merging is done from the end to the beginning of nums1 to avoid overwriting
unprocessed elements.
Returns:
None: The result is directly stored in nums1.
"""
# Set pointers for nums1, nums2, and the merge position in nums1
i = m - 1 # Pointer for the last valid element in nums1
j = n - 1 # Pointer for the last element in nums2
k = m + n - 1 # Pointer for the last position in nums1 (where merge happens)
# Merge in reverse order: compare elements from the back and place the larger one at the k-th position.
while j >= 0: # We only need to check nums2 since nums1's remaining items are already in place if nums2 is exhausted.
# Check if there are still elements in nums1 and if the current element in nums1 is larger than that in nums2.
if i >= 0 and nums1[i] > nums2[j]:
nums1[k] = nums1[i]
i -= 1 # Move the pointer in nums1 to the left.
else:
nums1[k] = nums2[j]
j -= 1 # Move the pointer in nums2 to the left.
k -= 1 # Move the merge pointer to the left for the next placement.
Summary of Techniques and Approaches:
These techniques are broadly applicable to many problems involving merging sorted data, in-place operations, and efficient array manipulation.