merge_sorted_array

 1# @leet start
 2class Solution:
 3    def merge(self, nums1: list[int], m: int, nums2: list[int], n: int) -> None:
 4        """
 5        This question asks us to merge two sorted arrays, where one array has
 6        space for the other array.
 7
 8        To do this optimally, we can do this in reverse, where we have 3 pointers:
 9
10        1. Pointer that starts at the end of the array
11        2. One pointer at the end of nums1
12        3. One pointer at the end of nums2
13
14        We compare the two pointers at nums1 and nums2, and we put the larger
15        one at the end of the array and decrement the pointer that had the larger
16        number.
17
18        We do this, while making sure to handle edge cases, like when we've exhausted
19        the numbers in either array. If that's the case, we want to put the other
20        number into the right place.
21        """
22        i = m - 1
23        j = n - 1
24
25        for k in range(m + n - 1, -1, -1):
26            if i < 0 or (j >= 0 and nums1[i] < nums2[j]):
27                nums1[k] = nums2[j]
28                j -= 1
29            else:
30                nums1[k] = nums1[i]
31                i -= 1
32
33
34# @leet end
35
36
37def test():
38    assert 2 + 2 == 4
class Solution:
 3class Solution:
 4    def merge(self, nums1: list[int], m: int, nums2: list[int], n: int) -> None:
 5        """
 6        This question asks us to merge two sorted arrays, where one array has
 7        space for the other array.
 8
 9        To do this optimally, we can do this in reverse, where we have 3 pointers:
10
11        1. Pointer that starts at the end of the array
12        2. One pointer at the end of nums1
13        3. One pointer at the end of nums2
14
15        We compare the two pointers at nums1 and nums2, and we put the larger
16        one at the end of the array and decrement the pointer that had the larger
17        number.
18
19        We do this, while making sure to handle edge cases, like when we've exhausted
20        the numbers in either array. If that's the case, we want to put the other
21        number into the right place.
22        """
23        i = m - 1
24        j = n - 1
25
26        for k in range(m + n - 1, -1, -1):
27            if i < 0 or (j >= 0 and nums1[i] < nums2[j]):
28                nums1[k] = nums2[j]
29                j -= 1
30            else:
31                nums1[k] = nums1[i]
32                i -= 1
def merge(self, nums1: list[int], m: int, nums2: list[int], n: int) -> None:
 4    def merge(self, nums1: list[int], m: int, nums2: list[int], n: int) -> None:
 5        """
 6        This question asks us to merge two sorted arrays, where one array has
 7        space for the other array.
 8
 9        To do this optimally, we can do this in reverse, where we have 3 pointers:
10
11        1. Pointer that starts at the end of the array
12        2. One pointer at the end of nums1
13        3. One pointer at the end of nums2
14
15        We compare the two pointers at nums1 and nums2, and we put the larger
16        one at the end of the array and decrement the pointer that had the larger
17        number.
18
19        We do this, while making sure to handle edge cases, like when we've exhausted
20        the numbers in either array. If that's the case, we want to put the other
21        number into the right place.
22        """
23        i = m - 1
24        j = n - 1
25
26        for k in range(m + n - 1, -1, -1):
27            if i < 0 or (j >= 0 and nums1[i] < nums2[j]):
28                nums1[k] = nums2[j]
29                j -= 1
30            else:
31                nums1[k] = nums1[i]
32                i -= 1

This question asks us to merge two sorted arrays, where one array has space for the other array.

To do this optimally, we can do this in reverse, where we have 3 pointers:

  1. Pointer that starts at the end of the array
  2. One pointer at the end of nums1
  3. One pointer at the end of nums2

We compare the two pointers at nums1 and nums2, and we put the larger one at the end of the array and decrement the pointer that had the larger number.

We do this, while making sure to handle edge cases, like when we've exhausted the numbers in either array. If that's the case, we want to put the other number into the right place.

def test():
38def test():
39    assert 2 + 2 == 4