-
Notifications
You must be signed in to change notification settings - Fork 43
/
merge-sorted-array.py
291 lines (258 loc) · 8.43 KB
/
merge-sorted-array.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
"""
88. Merge Sorted Array
Easy
You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing the number of elements in nums1 and nums2 respectively.
Merge nums1 and nums2 into a single array sorted in non-decreasing order.
The final sorted array should not be returned by the function, but instead be stored inside the array nums1. To accommodate this, nums1 has a length of m + n, where the first m elements denote the elements that should be merged, and the last n elements are set to 0 and should be ignored. nums2 has a length of n.
Example 1:
Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
Output: [1,2,2,3,5,6]
Explanation: The arrays we are merging are [1,2,3] and [2,5,6].
The result of the merge is [1,2,2,3,5,6] with the underlined elements coming from nums1.
Example 2:
Input: nums1 = [1], m = 1, nums2 = [], n = 0
Output: [1]
Explanation: The arrays we are merging are [1] and [].
The result of the merge is [1].
Example 3:
Input: nums1 = [0], m = 0, nums2 = [1], n = 1
Output: [1]
Explanation: The arrays we are merging are [] and [1].
The result of the merge is [1].
Note that because m = 0, there are no elements in nums1. The 0 is only there to ensure the merge result can fit in nums1.
Constraints:
nums1.length == m + n
nums2.length == n
0 <= m, n <= 200
1 <= m + n <= 200
-109 <= nums1[i], nums2[j] <= 109
Follow up: Can you come up with an algorithm that runs in O(m + n) time?
"""
# V0
# IDEA : 2 pointers
### NOTE : we need to merge the sorted arrat to nums1 with IN PLACE (CAN'T USE EXTRA CACHE)
# -> SO WE START FROM RIGHT HAND SIDE (biggeest element) to LEFT HAND SIDE (smallest element)
# -> Then paste the remain elements
class Solution(object):
def merge(self, nums1, m, nums2, n):
### NOTE : we define 2 pointers (p, q) here
p, q = m-1, n-1
### NOTE : the while loop conditions
while p >= 0 and q >= 0:
if nums1[p] > nums2[q]:
#***** NOTE : WE START FROM p+q+1 index, since that's the count of non-zero elements in nums1, and nums2
nums1[p+q+1] = nums1[p]
p = p-1
else:
### NOTE WE START FROM p+q+1 index, reason same as above
nums1[p+q+1] = nums2[q]
q = q-1
# if there're still elements in nums2, we just replace the ones in nums1[:q+1] with them (nums2[:q+1])
nums1[:q+1] = nums2[:q+1]
# V0'
# IDEA : 2 pointers + merge sort
# https://github.com/yennanliu/CS_basics/blob/master/algorithm/python/merge_sort.py
# TRICK : START FROM BIGGEST -> SMALLEST
### NOTE : we need to merge the sorted arrat to nums1 with IN PLACE (CAN'T USE EXTRA CACHE)
# -> SO WE START FROM RIGHT HAND SIDE (biggeest element) to LEFT HAND SIDE (smallest element)
# -> Then paste the remain elements
class Solution:
def merge(self, A, m, B, n):
pos = m + n - 1
i = m - 1
j = n - 1
while i >= 0 and j >= 0 :
if A[i]>B[j] :
A[pos]=A[i]
pos-=1
i-=1
else :
A[pos]=B[j]
pos-=1
j-=1
while i >= 0 :
A[pos] = A[i]
pos-=1
i-=1
while j >= 0:
A[pos] = B[j]
pos-=1
j-=1
# V0''
# IDEA : 2 pointers
class Solution:
def merge(self, nums1, m, nums2, n):
# below piece of code for edge case handling is not necessary
if m == 0:
nums1[:n] = nums2
i = m-1
j = n-1
# idx is also not nessary, we can replace it with i+j+1
idx = i + j + 1
while idx > 0 and j >= 0 and i >= 0:
if nums1[i] < nums2[j]:
nums1[idx] = nums2[j]
j-=1
else:
nums1[idx] = nums1[i]
i-=1
idx -= 1
# if j >= 0 condition is not necessary as well
if j >= 0:
nums1[:j+1] = nums2[:j+1]
# V0'''
# IDEA : via sorted func (may not be accepted)
class Solution(object):
def merge(self, nums1, m, nums2, n):
return sorted(nums1 + nums2)
# V1
# https://blog.csdn.net/coder_orz/article/details/51681144
# IDEA : 3 POINTERS
class Solution(object):
def merge(self, nums1, m, nums2, n):
"""
:type nums1: List[int]
:type m: int
:type nums2: List[int]
:type n: int
:rtype: void Do not return anything, modify nums1 in-place instead.
"""
p, q, k = m-1, n-1, m+n-1
while p >= 0 and q >= 0:
if nums1[p] > nums2[q]:
nums1[k] = nums1[p]
p, k = p-1, k-1
else:
nums1[k] = nums2[q]
q, k = q-1, k-1
nums1[:q+1] = nums2[:q+1]
# V1'
# https://blog.csdn.net/coder_orz/article/details/51681144
# IDEA : 2 POINTERS
class Solution(object):
def merge(self, nums1, m, nums2, n):
"""
:type nums1: List[int]
:type m: int
:type nums2: List[int]
:type n: int
:rtype: void Do not return anything, modify nums1 in-place instead.
"""
p, q = m-1, n-1
while p >= 0 and q >= 0:
if nums1[p] > nums2[q]:
nums1[p+q+1] = nums1[p]
p = p-1
else:
nums1[p+q+1] = nums2[q]
q = q-1
nums1[:q+1] = nums2[:q+1]
# V1'
# https://www.jiuzhang.com/solution/merge-sorted-array/#tag-highlight-lang-python
class Solution:
"""
@param: A: sorted integer array A which has m elements, but size of A is m+n
@param: m: An integer
@param: B: sorted integer array B which has n elements
@param: n: An integer
@return: nothing
"""
def merge(self, A, m, B, n):
# write your code here
pos = m + n - 1
i = m - 1
j = n - 1
while i >= 0 and j >= 0 :
if A[i]>B[j] :
A[pos]=A[i]
pos-=1
i-=1
else :
A[pos]=B[j]
pos-=1
j-=1
while i >= 0 :
A[pos] = A[i]
pos-=1
i-=1
while j >= 0:
A[pos] = B[j]
pos-=1
j-=1
# V1''
# https://blog.csdn.net/liuxiao214/article/details/77856326
class Solution(object):
def merge(self, nums1, m, nums2, n):
"""
:type nums1: List[int]
:type m: int
:type nums2: List[int]
:type n: int
:rtype: void Do not return anything, modify nums1 in-place instead.
"""
i=m-1
j=n-1
k=m+n-1
while k>=0:
if (j<0 or nums1[i]>nums2[j]) and i>=0:
nums1[k]=nums1[i]
i-=1
else:
nums1[k]=nums2[j]
j-=1
k-=1
# V2
class Solution:
# @param A a list of integers
# @param m an integer, length of A
# @param B a list of integers
# @param n an integer, length of B
# @return nothing
def merge(self, A, m, B, n):
last, i, j = m + n - 1, m - 1, n - 1
while i >= 0 and j >= 0:
if A[i] > B[j]:
A[last] = A[i]
last, i = last - 1, i - 1
else:
A[last] = B[j]
last, j = last - 1, j - 1
while j >= 0:
A[last] = B[j]
last, j = last - 1, j - 1
# if __name__ == "__main__":
# A = [1, 3, 5, 0, 0, 0, 0]
# B = [2, 4, 6, 7]
# Solution().merge(A, 3, B, 4)
# print(A)
# V3
# Time: O(n)
# Space: O(n)
# you may get a input like this,
# nums1 : [0]
# m : 0
# nums2 : [1]
# n : 1
# so you need to judge if n is still large than 0
class Solution2:
def merge(self, nums1, m, nums2, n):
# Space: O(n),
# Reference:
# - https://stackoverflow.com/questions/4948293/python-slice-assignment-memory-usage
# - https://stackoverflow.com/questions/10623302/how-assignment-works-with-python-list-slice
"""
:type nums1: List[int]
:type m: int
:type nums2: List[int]
:type n: int
:rtype: void Do not return anything, modify nums1 in-place instead.
"""
while m > 0 and n > 0:
if nums1[m-1] > nums2[n-1]:
nums1[m+n-1] = nums1[m-1]
m -= 1
else:
nums1[m+n-1] = nums2[n-1]
n -= 1
if n > 0:
nums1[:n] = nums2[:n]