Python 3 solution -- Editorial


#1
class Solution:
    # @param A : tuple of integers
    # @param B : tuple of integers
    # @return a double
    def findMedianSortedArrays(self, A, B):
        
        if len(A) > len(B):
            return self.findMedianSortedArrays(B, A)
        
        start_a = 0
        end_a = len(A)
        
        start_b = 0
        end_b = len(B)
        
        while(start_a <= end_a):
            position_a = (start_a + end_a)//2
            position_b = (len(A) + len(B) + 1)//2 - position_a
            
            max_left_a = float('-inf') if position_a == 0 else A[position_a -1]
            min_right_a = float('inf') if position_a == len(A) else A[position_a]
            
            
            max_left_b = float('-inf') if position_b == 0 else B[position_b -1]
            min_right_b = float('inf') if position_b == len(B) else B[position_b]
            
            if (max_left_a <= min_right_b and max_left_b <= min_right_a):
                if (len(A)+len(B))%2 == 0:
                    return avg(max(max_left_a ,max_left_b) + min(min_right_a , min_right_b))
                else:
                    return max(max_left_a,max_left_b)
                    
            elif max_left_a > min_right_b:
                end_a = position_a -1 
            else:
                start_a = position_a + 1