-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0004.py
More file actions
27 lines (23 loc) · 957 Bytes
/
0004.py
File metadata and controls
27 lines (23 loc) · 957 Bytes
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
class Solution:
def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
A, B = nums1, nums2
total = len(nums1) + len(nums2)
half = total // 2
if len(A) > len(B):
A, B = B, A
l, r = 0, len(A) - 1
while True:
i = (l + r) // 2
j = half - i - 2
a_left = A[i] if i >= 0 else float('-inf')
a_right = A[i + 1] if i + 1 < len(A) else float('inf')
b_left = B[j] if j >= 0 else float('-inf')
b_right = B[j + 1] if j + 1 < len(B) else float('inf')
if b_right >= a_left and a_right >= b_left:
if total % 2 == 1:
return min(a_right, b_right)
return (min(a_right, b_right) + max(a_left, b_left)) / 2
elif a_left > b_right:
r = i - 1
else:
l = i + 1