Short Python

  • 2

    Since all arrays are sorted, just find the MAX and MIN from the head and the tail. Remember keep track of the index of MAX and MIN since the two numbers can NOT be in the same array.

    class Solution(object):
        def maxDistance(self, arrays):
            :type arrays: List[List[int]]
            :rtype: int
            m = len(arrays)
            MAX, max_i = max([(arrays[i][-1], i) for i in range(m)])
            MIN, min_i = min([(arrays[i][0], i) for i in range(m)])
            a = max(abs(arrays[i][0] - MAX) for i in range(m) if i != max_i)
            b = max(abs(arrays[i][-1] - MIN) for i in range(m) if i != min_i)
            return max(a, b)

Log in to reply

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