Home » Python » [leetcode ][4] Median of Two Sorted A

[leetcode ][4] Median of Two Sorted A


There, are, two, sorted, nums1, and, of, size, m, and, N, respectively., Find, the, of, the, two, sorted, arrays., nums2, The, overall, run, time, complexity, should, arrays, be, O (log (m+n)).
. Median
https://leetcode.com/problems/median-of-two-sorted-arrays/
"
Ideas: using two points to find


class Solution (object) : 
def, findMedianSortedArrays (self, nums1, nums2) :
"""
: type, nums1:, List[int]
: type, nums2:, List[int]
: rtype: float
"" "
"
M = len (nums1); n = len (nums2)
if (m+n)%2 = = 1:
return, self.binarySearch (nums1, m, nums2, N, (m+n) /2+1)
else:
return, float ((self.binarySearch, nums1, m, nums2, N, (M +, n) / 2) + self.binarySearch (nums1, m, nums2, N, 2 (M + n) / 2 + 1))
def, binarySearch (self, nums1, m, nums2, N, K) :
if m<=0:
return nums2[k-1]
if n<=0:
return nums1[k-1]
if k<=1:
return min (nums1[0], nums2[0])
if nums1[m/2] > nums2[n/2]:
if (m/2 n/2 + +1 > = k:)
return, self.binarySearch (nums1, m/2, nums2, N, K)
else:
return, self.binarySearch (nums1, m, nums2[n/2+1: n], n-n/2-1, K - n/2, -1)
else:
if (m/2 n/2 + +1 > = k:)
return, self.binarySearch (nums1, m, nums2, n/2, K)
else:
return, self.binarySearch (nums1[m/2+1: m], m-m/2-1, nums2, N, k-m/2-1)

Latest