Python solution Using Gradient Search


#1
class Solution:
    # @param A : list of integers
    # @param B : list of integers
    # @param C : list of integers
    # @return an integer
    def solve(self, A, B, C):
        i = 0
        j = 0
        k = 0
        
        min_diff = 2147483647
        
        def min_diff_cal(a,b,c):
            return abs(max(a,b,c)-min(a,b,c))
        
        while(i < len(A) and j < len(B) and k < len(C)):
            a=A[i]
            b=B[j]
            c=C[k]
            
            new_diff = min_diff_cal(a,b,c)
            if new_diff < min_diff:
                min_diff = new_diff
            if min_diff == 0:
                return min_diff
            
            #Gradient search
            
            new_diff_a = min_diff_cal(A[min(i+1,len(A)-1)],B[j],C[k])
            new_diff_b = min_diff_cal(A[i],B[min(j+1,len(B)-1)],C[k])
            new_diff_c = min_diff_cal(A[i],B[j],C[min(k+1,len(C)-1)])
            
            min_new = min(new_diff_a, new_diff_b, new_diff_c)
            
            if min_new == new_diff_a:
                i += 1
            elif min_new == new_diff_b:
                j += 1
            elif min_new == new_diff_c:
                k += 1
        
        return min_diff

This solution just looks for the next best possible option and increments the pointers in that direction.