1266. Minimum Time Visiting All Points

On a plane there are n points with integer coordinates points[i] = [xi, yi]. Your task is to find the minimum time in seconds to visit all points.

You can move according to the next rules:

  • In one second always you can either move vertically, horizontally by one unit or diagonally (it means to move one unit vertically and one unit horizontally in one second).
  • You have to visit the points in the same order as they appear in the array.

给出一堆的二维点,按顺序走完这些点最少需要多少步,每步可以横竖斜三种方式走,实际上,最小步数就等于横坐标之差和纵坐标之差的最大值。

class Solution(object):
    def minTimeToVisitAllPoints(self, points):
        """
        :type points: List[List[int]]
        :rtype: int
        """
        ans = 0
        for i in range(1, len(points), 1):
            step = abs(points[i][0] - points[i - 1][0])
            step = max(abs(points[i][1] - points[i - 1][1]), step)
            ans += step
        return ans
            
        
原文地址:https://www.cnblogs.com/whatyouthink/p/13205498.html