forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
median-of-two-sorted-arrays.py
119 lines (101 loc) · 3.49 KB
/
median-of-two-sorted-arrays.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
# Time: O(log(min(m, n)))
# Space: O(1)
class Solution(object):
def findMedianSortedArrays(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: float
"""
len1, len2 = len(nums1), len(nums2)
if (len1 + len2) % 2 == 1:
return self.getKth(nums1, nums2, (len1 + len2)/2 + 1)
else:
return (self.getKth(nums1, nums2, (len1 + len2)/2) +
self.getKth(nums1, nums2, (len1 + len2)/2 + 1)) * 0.5
def getKth(self, A, B, k):
m, n = len(A), len(B)
if m > n:
m, n = n, m
A, B = B, A
left, right = 0, m - 1
while left <= right:
mid = left + (right - left) / 2
if 0 <= k - 1 - mid < n and A[mid] >= B[k - 1 - mid]:
right = mid - 1
else:
left = mid + 1
Ai_minus_1 = A[left - 1] if left - 1 >= 0 else float("-inf")
Bj = B[k - 1 - left] if k - 1 - left >= 0 else float("-inf")
return max(Ai_minus_1, Bj)
# Time: O(log(max(m, n)) * log(max_val - min_val))
# Space: O(1)
# Generic solution.
class Solution_Generic(object):
def findMedianSortedArrays(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: float
"""
array = [nums1, nums2]
total = sum(len(nums) for nums in array)
if total % 2 == 1:
return self.getKth(array, total//2 + 1)
else:
return (self.getKth(array, total//2) +
self.getKth(array, total//2 + 1)) * 0.5
def getKth(self, arrays, k):
def binary_search(array, left, right, target, check):
while left <= right:
mid = left + (right-left)//2
if check(array, mid, target):
right = mid-1
else:
left = mid+1
return left
def check(arrays, num, target):
res = 0
for array in arrays:
if array: # count the number of values which are less or equal to num
res += binary_search(array, 0, len(array) - 1, num,
lambda array, x, y: array[x] > y)
return res >= target
left, right = float("inf"), float("-inf")
for array in arrays:
if array:
left = min(left, array[0])
right = max(right, array[-1])
return binary_search(arrays, left, right, k, check)
class Solution_3(object):
def findMedianSortedArrays(self, A, B):
if A is None and B is None:
return -1.0
lenA = len(A)
lenB = len(B)
lenn = lenA + lenB
indexA,indexB,indexC = 0,0,0
C = [False for i in xrange(lenn)]
while indexA < lenA and indexB < lenB:
if A[indexA] < B[indexB]:
C[indexC] = A[indexA]
indexC += 1
indexA += 1
else:
C[indexC] = B[indexB]
indexC += 1
indexB += 1
while indexA < lenA:
C[indexC] = A[indexA]
indexC += 1
indexA += 1
while indexB < lenB:
C[indexC] = B[indexB]
indexC += 1
indexB += 1
indexM1 = (lenn - 1) / 2
indexM2 = lenn / 2
if (lenn % 2 == 0):
return (C[indexM1] + C[indexM2]) / 2.0
else:
return C[indexM2] / 1.0