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