O(n) python solution beats 100%


  • 0
    V

    The idea is that:the biggest distance between two array is equal to abs(a[0] -history_max)or abs(a[-1]-history_min)

    def maxDistance(self, arrays):
            """
            :type arrays: List[List[int]]
            :rtype: int
            """
      
            
            maxdis=-1
            lmax,lmin=arrays[0][-1],arrays[0][0]
            
            for index in range(1,len(arrays)):
                maxdis=max(abs(lmax-arrays[index][0]),maxdis)
                maxdis=max(abs(lmin-arrays[index][-1]),maxdis)
                
                lmax,lmin=max(lmax,arrays[index][-1]),min(lmin,arrays[index][0])
                
            return maxdis
    

Log in to reply
 

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