Skip to content

Commit 88e2a3f

Browse files
committed
Time: 0 ms (100%), Space: 18.1 MB (26.01%) - LeetHub
1 parent 983e77c commit 88e2a3f

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# time complexity: O(n)
2+
# space complexity: O(1)
3+
from typing import List
4+
5+
6+
class Solution:
7+
def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
8+
idx1 = m - 1
9+
idx2 = n - 1
10+
currIdx = m + n - 1
11+
12+
while idx2 >= 0:
13+
if idx1 >= 0 and nums1[idx1] > nums2[idx2]:
14+
nums1[currIdx] = nums1[idx1]
15+
idx1 -= 1
16+
else:
17+
nums1[currIdx] = nums2[idx2]
18+
idx2 -= 1
19+
currIdx -= 1
20+
21+
22+
nums1 = [1, 2, 3, 0, 0, 0]
23+
m = 3
24+
nums2 = [2, 5, 6]
25+
n = 3
26+
print(Solution().merge(nums1, m, nums2, n))
27+
nums1 = [1]
28+
m = 1
29+
nums2 = []
30+
n = 0
31+
print(Solution().merge(nums1, m, nums2, n))
32+
nums1 = [0]
33+
m = 0
34+
nums2 = [1]
35+
n = 1
36+
print(Solution().merge(nums1, m, nums2, n))

0 commit comments

Comments
 (0)