Skip to content

Commit 79aa85b

Browse files
authored
Update median-of-two-sorted-arrays.py
1 parent e5dff8f commit 79aa85b

File tree

1 file changed

+34
-87
lines changed

1 file changed

+34
-87
lines changed

Python/median-of-two-sorted-arrays.py

Lines changed: 34 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,28 @@ def findMedianSortedArrays(self, nums1, nums2):
88
:type nums2: List[int]
99
:rtype: float
1010
"""
11-
len1, len2 = len(nums1), len(nums2)
12-
if (len1 + len2) % 2 == 1:
13-
return self.getKth(nums1, nums2, (len1 + len2)/2 + 1)
14-
else:
15-
return (self.getKth(nums1, nums2, (len1 + len2)/2) +
16-
self.getKth(nums1, nums2, (len1 + len2)/2 + 1)) * 0.5
17-
18-
def getKth(self, A, B, k):
19-
m, n = len(A), len(B)
20-
if m > n:
21-
m, n = n, m
22-
A, B = B, A
23-
24-
left, right = 0, m - 1
25-
while left <= right:
26-
mid = left + (right - left) / 2
27-
if 0 <= k - 1 - mid < n and A[mid] >= B[k - 1 - mid]:
28-
right = mid - 1
29-
else:
30-
left = mid + 1
11+
def binary_search(left, right, check):
12+
while left <= right:
13+
mid = left+(right-left)//2
14+
if check(mid):
15+
right = mid-1
16+
else:
17+
left = mid+1
18+
return left
3119

32-
Ai_minus_1 = A[left - 1] if left - 1 >= 0 else float("-inf")
33-
Bj = B[k - 1 - left] if k - 1 - left >= 0 else float("-inf")
20+
def getKth(A, B, k):
21+
m, n = len(A), len(B)
22+
if m > n:
23+
m, n = n, m
24+
A, B = B, A
25+
i = binary_search(0, min(m, k)-1, lambda i: 0 <= k-1-i < n and A[i] >= B[k-1-i])
26+
return max(A[i-1] if i-1 >= 0 else float("-inf"), B[k-1-i] if k-1-i >= 0 else float("-inf"))
3427

35-
return max(Ai_minus_1, Bj)
28+
len1, len2 = len(nums1), len(nums2)
29+
if (len1+len2) % 2 == 1:
30+
return getKth(nums1, nums2, (len1+len2)//2+1)
31+
else:
32+
return (getKth(nums1, nums2, (len1+len2)//2)+getKth(nums1, nums2, (len1+len2)//2+1))*0.5
3633

3734

3835
# Time: O(log(max(m, n)) * log(max_val - min_val))
@@ -45,75 +42,25 @@ def findMedianSortedArrays(self, nums1, nums2):
4542
:type nums2: List[int]
4643
:rtype: float
4744
"""
48-
array = [nums1, nums2]
49-
total = sum(len(nums) for nums in array)
50-
if total % 2 == 1:
51-
return self.getKth(array, total//2 + 1)
52-
else:
53-
return (self.getKth(array, total//2) +
54-
self.getKth(array, total//2 + 1)) * 0.5
55-
56-
def getKth(self, arrays, k):
57-
def binary_search(array, left, right, target, check):
45+
def binary_search(left, right, check):
5846
while left <= right:
59-
mid = left + (right-left)//2
60-
if check(array, mid, target):
47+
mid = left+(right-left)//2
48+
if check(mid):
6149
right = mid-1
6250
else:
6351
left = mid+1
6452
return left
6553

66-
def check(arrays, num, target):
67-
res = 0
68-
for array in arrays:
69-
if array: # count the number of values which are less or equal to num
70-
res += binary_search(array, 0, len(array) - 1, num,
71-
lambda array, x, y: array[x] > y)
72-
return res >= target
73-
74-
left, right = float("inf"), float("-inf")
75-
for array in arrays:
76-
if array:
77-
left = min(left, array[0])
78-
right = max(right, array[-1])
79-
return binary_search(arrays, left, right, k, check)
80-
81-
class Solution_3(object):
82-
def findMedianSortedArrays(self, A, B):
83-
84-
if A is None and B is None:
85-
return -1.0
86-
lenA = len(A)
87-
lenB = len(B)
88-
lenn = lenA + lenB
89-
90-
indexA,indexB,indexC = 0,0,0
91-
C = [False for i in xrange(lenn)]
92-
while indexA < lenA and indexB < lenB:
93-
if A[indexA] < B[indexB]:
94-
C[indexC] = A[indexA]
95-
indexC += 1
96-
indexA += 1
97-
else:
98-
C[indexC] = B[indexB]
99-
indexC += 1
100-
indexB += 1
101-
102-
while indexA < lenA:
103-
C[indexC] = A[indexA]
104-
indexC += 1
105-
indexA += 1
54+
def getKth(arrays, k):
55+
def check(num):
56+
# count the number of values which are less or equal to num
57+
return sum(binary_search(0, len(arr)-1, lambda x: arr[x] > num) for arr in arrays) >= k
58+
59+
return binary_search(min(arr[0] for arr in arrays if arr), max(arr[-1] for arr in arrays if arr), check)
10660

107-
while indexB < lenB:
108-
C[indexC] = B[indexB]
109-
indexC += 1
110-
indexB += 1
111-
112-
indexM1 = (lenn - 1) / 2
113-
indexM2 = lenn / 2
114-
115-
if (lenn % 2 == 0):
116-
return (C[indexM1] + C[indexM2]) / 2.0
61+
array = [nums1, nums2]
62+
total = sum(len(nums) for nums in array)
63+
if total % 2 == 1:
64+
return getKth(array, total//2+1)
11765
else:
118-
return C[indexM2] / 1.0
119-
66+
return (getKth(array, total//2)+getKth(array, total//2+1))*0.5

0 commit comments

Comments
 (0)