Beats 100% now by comparing four elements(max1,max2,min1,min2)


  • 0
    Q

    class Solution(object):
    def maxDistance(self, arrays):
    """
    :type arrays: List[List[int]]
    :rtype: int
    """
    tempmax = []
    tempmin = []
    hashmax = {}
    hashmin = {}
    for i in xrange(len(arrays)):
    l = len(arrays[i])
    tempmax.append(arrays[i][l-1])
    tempmin.append(arrays[i][0])
    hashmax[arrays[i][l-1]] = i
    hashmin[arrays[i][0]] = i

        tempmax.sort()
        tempmin.sort()
        mmax1 = tempmax[len(tempmax)-1]
        mmax2 = tempmax[len(tempmax)-2]
        mmin1 = tempmin[0]
        mmin2 = tempmin[1]
        if hashmax[mmax1] != hashmin[mmin1]:
            return mmax1 - mmin1
        else:
            return max(mmax1 - mmin2, mmax2 - mmin1)

Log in to reply
 

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