-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path004-MedianOfTwoSortedArrays.py
30 lines (28 loc) · 1.07 KB
/
004-MedianOfTwoSortedArrays.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
# There are two sorted arrays nums1 and nums2 of size m and n respectively.
# Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
# You may assume nums1 and nums2 cannot be both empty.
class Solution:
def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
merged = self.merge(nums1, nums2)
mid = (len(nums1) + len(nums2) - 1) // 2
if (len(nums1) + len(nums2)) % 2 == 0:
return (merged[mid] + merged[mid + 1]) / 2
else:
return merged[mid]
def merge(self, nums1: List[int], nums2: List[int]): -> List[int]
merged = []
i = j = 0
while i < len(nums1) and j < len(nums2):
if nums1[i] <= nums2[j]:
merged.append(nums1[i])
i += 1
else:
merged.append(nums2[j])
j += 1
while i < len(nums1):
merged.append(nums1[i])
i += 1
while j < len(nums2):
merged.append(nums2[j])
j += 1
return merged