Squirrel Simulation

这个题其实根本没什么复杂算法,思路非常简单,我们现在就是要找一个点,这个点的松子到松鼠的距离加上松子到树的距离再加上其他松子到树距离的两倍,这个值是最小的,那既然这样,我们完全可以把所有松子到树的距离加起来乘以两倍,同时再遍历一遍松子到松鼠的距离,通过修改这个基准值找到其中一个最小的值就好了

class Solution(object):
    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
        """
        cumDistance = 0
        for i, j in nuts:
            cumDistance += (abs(i - tree[0]) + abs(j - tree[1]))
        cumDistance *= 2
        result = sys.maxint
        for i, j in nuts:
            tmp = cumDistance - (abs(i - tree[0]) + abs(j - tree[1]))
            tmp += (abs(i - squirrel[0]) + abs(j - squirrel[1]))
            result = min(tmp, result)
        return result

results matching ""

    No results matching ""