Skip to content

Commit 00f9ac8

Browse files
committed
LeetCode. 88. Merge Sorted Array
1 parent 9aa5ec3 commit 00f9ac8

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Proposed solutions to some LeetCode problems. The first column links to the prob
1515
| [29. Divide two integers][lc29] | Medium | [python](leetcode/divide_two_integers.py) |
1616
| [51. N-Queens][lc51] | Hard | [python](leetcode/n-queens.py) |
1717
| [52. N-Queens II][lc52] | Hard | [python](leetcode/n-queens-ii.py) |
18+
| [88. Merge Sorted Array][lc88] | Easy | [python](leetcode/merge-sorted-array.py) |
1819
| [160. Intersection of Two Linked Lists][lc160] | Easy | [python](leetcode/intersection-of-two-linked-lists.py) |
1920
| [304. Range Sum Query 2D - Immutable][lc304] | Medium | [python](leetcode/divide_two_integers.py) |
2021
| [596. Classes More Than 5 Students][lc596] | Easy | [mysql](leetcode/classes_more_than_5_students.sql) |
@@ -28,6 +29,7 @@ Proposed solutions to some LeetCode problems. The first column links to the prob
2829
[lc29]: https://leetcode.com/problems/divide-two-integers/
2930
[lc51]: https://leetcode.com/problems/n-queens/
3031
[lc52]: https://leetcode.com/problems/n-queens-ii/
32+
[lc88]: https://leetcode.com/problems/merge-sorted-array/
3133
[lc160]: https://leetcode.com/problems/intersection-of-two-linked-lists/
3234
[lc304]: https://leetcode.com/problems/range-sum-query-2d-immutable/
3335
[lc596]: https://leetcode.com/problems/classes-more-than-5-students/

leetcode/merge-sorted-array.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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

Comments
 (0)