generated from zeikar/issueage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedian-of-two-sorted-arrays.py
35 lines (26 loc) · 1.16 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
from typing import List
class Solution:
def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
if len(nums1) > len(nums2):
nums1, nums2 = nums2, nums1
nums1 = [-9999999] + nums1 + [9999999]
nums2 = [-9999999] + nums2 + [9999999]
low = 0
high = len(nums1)
while low <= high:
partition_x = (low + high) // 2
partition_y = (len(nums1) + len(nums2) + 1) // 2 - partition_x
max_left_x = nums1[max(0, partition_x - 1)]
min_right_x = nums1[min(len(nums1) - 1, partition_x)]
max_left_y = nums2[max(0, partition_y - 1)]
min_right_y = nums2[min(len(nums2) - 1, partition_y)]
if max_left_x <= min_right_y and min_right_x >= max_left_y:
if (len(nums1) + len(nums2)) % 2 == 0:
return (max(max_left_x, max_left_y) + min(min_right_x, min_right_y)) / 2
else:
return max(max_left_x, max_left_y)
if min_right_x < max_left_y:
low = partition_x + 1
else:
high = partition_x - 1
return 0.0