-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path065 Median of two sorted arrays.py
50 lines (44 loc) · 1.08 KB
/
065 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
from bisect import *
def median(a: int, b: int) -> float:
x = min(a[0],b[0])
y = max(a[-1],b[-1])
n = len(a)
m = len(b)
if (n+m)%2:
i = x
j = y
ans = -1
while i<=j:
mid = i+(j-i)//2;
aa = bisect_right(a,mid)
bb = bisect_right(b,mid)
if (aa+bb) > (n+m)//2:
j = mid-1
else:
i = mid+1
return float(i)
else:
i = x
j = y
ans = -1
while i<=j:
mid = i+(j-i)//2;
aa = bisect_right(a,mid)
bb = bisect_right(b,mid)
if (aa+bb) > (n+m)//2:
j = mid-1
else:
i = mid+1
f = i
i = x
j = y
ans = -1
while i<=j:
mid = i+(j-i)//2;
aa = bisect_right(a,mid)
bb = bisect_right(b,mid)
if (aa+bb) > (n+m)//2-1:
j = mid-1
else:
i = mid+1
return (f+i)/2