-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathsolution.py
More file actions
23 lines (21 loc) · 650 Bytes
/
Copy pathsolution.py
File metadata and controls
23 lines (21 loc) · 650 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution:
# Time: O(m + n)
# Space: O(1)
def merge(self, nums1: list[int], m: int, nums2: list[int], n: int) -> None:
# Fill from the back to avoid overwriting nums1's real elements
index = m + n - 1
i = m - 1
j = n - 1
while i >= 0 and j >= 0:
if nums1[i] > nums2[j]:
nums1[index] = nums1[i]
i -= 1
else:
nums1[index] = nums2[j]
j -= 1
index -= 1
# Only nums2 leftovers can remain
while j >= 0:
nums1[index] = nums2[j]
j -= 1
index -= 1