|
| 1 | +# https://leetcode.com/problems/merge-sorted-array/ |
| 2 | + |
| 3 | +from typing import List |
| 4 | +from helpers import BColors |
| 5 | + |
| 6 | + |
| 7 | +class Solution: |
| 8 | + def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None: |
| 9 | + # If n is 0, there is nothing to do |
| 10 | + if n > 0: |
| 11 | + # If m == 0 |
| 12 | + if m == 0: |
| 13 | + for i in range(n): |
| 14 | + nums1[i] = nums2[i] |
| 15 | + else: |
| 16 | + index1, index2 = 0, 0 |
| 17 | + remaining1 = m |
| 18 | + # Iterate over the elements in nums1 |
| 19 | + while index1 < m + n and index2 < n: |
| 20 | + print(f'» Checking {index1}:{index2}') |
| 21 | + # If the current value in nums2 is less than the current value in nums1 |
| 22 | + # or we have used all the valid values in nums1 |
| 23 | + if nums2[index2] < nums1[index1] or remaining1 == 0: |
| 24 | + print( |
| 25 | + f'» {nums1[index1]} > {nums2[index2]}; inserting at {index1}') |
| 26 | + # Insert from n2 at current position |
| 27 | + nums1[index1:index1] = [nums2[index2]] |
| 28 | + del nums1[-1] |
| 29 | + # Update the indexes |
| 30 | + index1 += 1 |
| 31 | + index2 += 1 |
| 32 | + else: |
| 33 | + print( |
| 34 | + f'» {nums1[index1]} <= {nums2[index2]}; skipping') |
| 35 | + # Move to the next element |
| 36 | + index1 += 1 |
| 37 | + remaining1 -= 1 |
| 38 | + print(f'» {nums1}') |
| 39 | + |
| 40 | + # This is a better solution! Starting from the tail |
| 41 | + def mergeFromTail(self, nums1, m, nums2, n): |
| 42 | + while n > 0: |
| 43 | + if m <= 0 or nums2[n-1] >= nums1[m-1]: |
| 44 | + nums1[m+n-1] = nums2[n-1] |
| 45 | + n -= 1 |
| 46 | + else: |
| 47 | + nums1[m+n-1] = nums1[m-1] |
| 48 | + m -= 1 |
| 49 | + |
| 50 | + # And this is a really cool solution using built-in functionality |
| 51 | + def mergeVeryShort(self, nums1, m, nums2, n): |
| 52 | + nums1[m:] = nums2[:n] |
| 53 | + nums1.sort() |
| 54 | + |
| 55 | + |
| 56 | +def test(): |
| 57 | + sol = Solution() |
| 58 | + nums1, m, nums2, n, expected = [1, 2, 3, 0, 0, 0], 3, [ |
| 59 | + 2, 5, 6], 3, [1, 2, 2, 3, 5, 6] |
| 60 | + sol.merge(nums1, m, nums2, n) |
| 61 | + assert nums1 == expected, f'{nums1} differs from {expected}' |
| 62 | + |
| 63 | + nums1, m, nums2, n, expected = [1], 1, [], 0, [1] |
| 64 | + sol.merge(nums1, m, nums2, n) |
| 65 | + assert nums1 == expected, f'{nums1} differs from {expected}' |
| 66 | + |
| 67 | + nums1, m, nums2, n, expected = [0], 0, [1], 1, [1] |
| 68 | + sol.merge(nums1, m, nums2, n) |
| 69 | + assert nums1 == expected, f'{nums1} differs from {expected}' |
| 70 | + |
| 71 | + nums1, m, nums2, n, expected = [2, 0], 1, [1], 1, [1, 2] |
| 72 | + sol.merge(nums1, m, nums2, n) |
| 73 | + assert nums1 == expected, f'{nums1} differs from {expected}' |
| 74 | + |
| 75 | + nums1, m, nums2, n, expected = [-1, 0, 0, 3, 3, 3, 0, |
| 76 | + 0, 0], 6, [1, 2, 2], 3, [-1, 0, 0, 1, 2, 2, 3, 3, 3] |
| 77 | + sol.merge(nums1, m, nums2, n) |
| 78 | + assert nums1 == expected, f'{nums1} differs from {expected}' |
| 79 | + |
| 80 | + print(f'\n{BColors.bold}{BColors.ok_green}» All tests passed!{BColors.end_dc}\n') |
| 81 | + |
| 82 | + |
| 83 | +test() |
| 84 | + |
| 85 | + |
| 86 | +def testFromTail(): |
| 87 | + sol = Solution() |
| 88 | + nums1, m, nums2, n, expected = [1, 2, 3, 0, 0, 0], 3, [ |
| 89 | + 2, 5, 6], 3, [1, 2, 2, 3, 5, 6] |
| 90 | + sol.mergeFromTail(nums1, m, nums2, n) |
| 91 | + assert nums1 == expected, f'{nums1} differs from {expected}' |
| 92 | + |
| 93 | + nums1, m, nums2, n, expected = [1], 1, [], 0, [1] |
| 94 | + sol.mergeFromTail(nums1, m, nums2, n) |
| 95 | + assert nums1 == expected, f'{nums1} differs from {expected}' |
| 96 | + |
| 97 | + nums1, m, nums2, n, expected = [0], 0, [1], 1, [1] |
| 98 | + sol.mergeFromTail(nums1, m, nums2, n) |
| 99 | + assert nums1 == expected, f'{nums1} differs from {expected}' |
| 100 | + |
| 101 | + nums1, m, nums2, n, expected = [2, 0], 1, [1], 1, [1, 2] |
| 102 | + sol.mergeFromTail(nums1, m, nums2, n) |
| 103 | + assert nums1 == expected, f'{nums1} differs from {expected}' |
| 104 | + |
| 105 | + nums1, m, nums2, n, expected = [-1, 0, 0, 3, 3, 3, 0, |
| 106 | + 0, 0], 6, [1, 2, 2], 3, [-1, 0, 0, 1, 2, 2, 3, 3, 3] |
| 107 | + sol.mergeFromTail(nums1, m, nums2, n) |
| 108 | + assert nums1 == expected, f'{nums1} differs from {expected}' |
| 109 | + |
| 110 | + print(f'\n{BColors.bold}{BColors.ok_green}» All tests passed!{BColors.end_dc}\n') |
| 111 | + |
| 112 | + |
| 113 | +testFromTail() |
0 commit comments