Skip to content

Commit 4a04223

Browse files
committed
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 17.8 MB (6.97%)
1 parent 3dc726e commit 4a04223

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
# Approach 3 - Three Pointers (Start from the End)
1+
# Approach 3: Three Pointers (Start from the end)
22

3-
# Time: O(m+n)
3+
# Time: O(n + m)
44
# Space: O(1)
55

66
class Solution:
77
def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
88
"""
99
Do not return anything, modify nums1 in-place instead.
1010
"""
11+
# Set p1 and p2 to point to the end of their respective arrays.
1112
p1 = m - 1
1213
p2 = n - 1
13-
14-
for p in range(n+m-1, -1, -1):
14+
15+
# And move p backward through the array, each time writing
16+
# the smallest value pointed at by p1 or p2.
17+
for p in range(n + m - 1, -1, -1):
1518
if p2 < 0:
1619
break
1720
if p1 >= 0 and nums1[p1] > nums2[p2]:
@@ -20,3 +23,4 @@ def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
2023
else:
2124
nums1[p] = nums2[p2]
2225
p2 -= 1
26+

0 commit comments

Comments
 (0)