-
Notifications
You must be signed in to change notification settings - Fork 0
/
merg_sorted_arr.py
61 lines (59 loc) · 1.47 KB
/
merg_sorted_arr.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
def merge(nums1, m, nums2, n):
"""
:type nums1: List[int]
:type m: int
:type nums2: List[int]
:type n: int
:rtype: None Do not return anything, modify nums1 in-place instead.
"""
# i = 0 #!MY CODE doesnt work
# j = 0
# mn = 0
# save = 1000000
# chk = min(m,n)
# while(i<chk and j<chk):
# mn = min(nums1[i], nums2[j], save)
# if mn == nums1[i]:
# i+=1
# elif mn == nums2[j]:
# save = nums1[i]
# nums1[i] = mn
# j+=1
# if j<n:
# while j<n:
# nums1[i] = min(nums2[j], save)
# if nums1[i] == save: save = 1000000
# else: j+=1
# i+=1
# elif i<m:
# while i<m:
# nums1[i], save = save, nums1[i]
# i+=1
ins = m+n-1 #!NEETCODE
i = m-1
j = n-1
while i>=0 and j>=0:
if nums1[i] > nums2[j]:
nums1[ins] = nums1[i]
i-=1
else:
nums1[ins] = nums2[j]
j-=1
ins-=1
#fill nums1 with leftover nums2
while j>=0:
nums1[ins] = nums2[j]
j, ins = j-1, ins-1
""" ins = m+n-1
i = m-1
j = n-1
#!for k in range(ins,-1,-1):
#! if i<=k:
if j<0:
return
if i>=0 and nums1[i] > nums2[j]:
nums1[k] = nums1[i]
i-=1
else:
nums1[k] = nums2[j]
j-=1 """