Python Solution


  • 0
    H
        def minDistance(self, height, width, tree, squirrel, nuts):
            """
            :type height: int
            :type width: int
            :type tree: List[int]
            :type squirrel: List[int]
            :type nuts: List[List[int]]
            :rtype: int
            """
            n=len(nuts)
            res=0
            dis=-sys.maxint-1
            idx=-1
            for i in xrange(n):
                dis2=abs(nuts[i][0]-squirrel[0])+abs(nuts[i][1]-squirrel[1])
                dis3=abs(nuts[i][0]-tree[0])+abs(nuts[i][1]-tree[1])
                if dis<dis3-dis2:
                    dis=dis3-dis2
                    idx=i
            for i in xrange(n):
                if i!=idx:
                    res+=2*(abs(nuts[i][0]-tree[0])+abs(nuts[i][1]-tree[1]))
            i=idx
            res+=abs(nuts[i][0]-tree[0])+abs(nuts[i][1]-tree[1])
            res+=abs(nuts[i][0]-squirrel[0])+abs(nuts[i][1]-squirrel[1])
                    
            return res
    

Log in to reply
 

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