Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def findMedianSortedArrays(self, A: List[int], B: List[int]) -> float:
- if len(A)>len(B):
- A,B=B,A
- m,n=len(A),len(B)
- tot=(m+n)
- half = (m+n)>>1
- l,r=0,len(A)-1
- while True:
- c1=(l+r)>>1#c1--cut1 c2--cut2
- c2=half-(c1+1)-1# making c1 to 1 indexed and than make c2 to 0 indexed
- aleft=A[c1] if c1>=0 else float('-inf')#out of bounds
- aright=A[c1+1] if c1+1<m else float('inf')
- bleft=B[c2] if c2>=0 else float('-inf')
- bright=B[c2+1] if c2+1<n else float('inf')
- if aleft<=bright and bleft<=aright:
- if (tot)%2==1:
- return min(aright,bright)#verify with example [3] [2,4] remember not max(aleft,bleft) when total is odd
- else:
- return (max(aleft,bleft)+min(aright,bright))/2
- elif aleft>bright:
- r=c1-1
- else:
- l=c1+1
Advertisement
Add Comment
Please, Sign In to add comment