class Solution:
# @return a float
def findTheNthNum(self, A, B, N):
if len(A)>len(B):
return self.findTheNthNum(B,A,N);
if len(A)==0:
return B[N-1];
if len(B)==0:
return A[N-1];
if N==1:
return min(A[0], B[0]);
half = N/2;
if len(A)<half:
B = B[half:len(B)]
return self.findTheNthNum(A, B, N-half)
if A[half-1]>B[half-1]:
B = B[half:len(B)]
return self.findTheNthNum(A, B, N-half)
else:
A = A[half:len(A)]
return self.findTheNthNum(A, B, N-half)
def findMedianSortedArrays(self, A, B):
total = len(A) + len(B);
if total%2!=0:
return self.findTheNthNum(A, B, total/2+1)
else:
return ( self.findTheNthNum( A, B, total/2) + self.findTheNthNum( A, B, total/2+1) )/2.0
# @return a float
def findTheNthNum(self, A, B, N):
if len(A)>len(B):
return self.findTheNthNum(B,A,N);
if len(A)==0:
return B[N-1];
if len(B)==0:
return A[N-1];
if N==1:
return min(A[0], B[0]);
half = N/2;
if len(A)<half:
B = B[half:len(B)]
return self.findTheNthNum(A, B, N-half)
if A[half-1]>B[half-1]:
B = B[half:len(B)]
return self.findTheNthNum(A, B, N-half)
else:
A = A[half:len(A)]
return self.findTheNthNum(A, B, N-half)
def findMedianSortedArrays(self, A, B):
total = len(A) + len(B);
if total%2!=0:
return self.findTheNthNum(A, B, total/2+1)
else:
return ( self.findTheNthNum( A, B, total/2) + self.findTheNthNum( A, B, total/2+1) )/2.0