-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path0088-merge-sorted-array.py
More file actions
36 lines (31 loc) · 771 Bytes
/
0088-merge-sorted-array.py
File metadata and controls
36 lines (31 loc) · 771 Bytes
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
# time complexity: O(n)
# space complexity: O(1)
from typing import List
class Solution:
def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
idx1 = m - 1
idx2 = n - 1
currIdx = m + n - 1
while idx2 >= 0:
if idx1 >= 0 and nums1[idx1] > nums2[idx2]:
nums1[currIdx] = nums1[idx1]
idx1 -= 1
else:
nums1[currIdx] = nums2[idx2]
idx2 -= 1
currIdx -= 1
nums1 = [1, 2, 3, 0, 0, 0]
m = 3
nums2 = [2, 5, 6]
n = 3
print(Solution().merge(nums1, m, nums2, n))
nums1 = [1]
m = 1
nums2 = []
n = 0
print(Solution().merge(nums1, m, nums2, n))
nums1 = [0]
m = 0
nums2 = [1]
n = 1
print(Solution().merge(nums1, m, nums2, n))