Python FASTEST solution (80-90ms)


  • 0
    A
    class Solution(object):
        def maxDistance(self, arrays):
            """
            :type arrays: List[List[int]]
            :rtype: int
            """
            max_ab, max_ba = 0, 0 
            n = len(arrays)
            running_min, running_max = arrays[0][0], arrays[0][-1]
            for i in range(1, n):
                max_ab = max(max_ab, running_max - arrays[i][0])
                running_max = max(running_max, arrays[i][-1])
                max_ba = max(max_ba, arrays[i][-1] - running_min)
                running_min = min(running_min, arrays[i][0])
            return max(max_ab, max_ba)
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.