美文网首页
1266. Minimum Time Visiting All

1266. Minimum Time Visiting All

作者: 30岁每天进步一点点 | 来源:发表于2020-03-06 18:15 被阅读0次

    附leetcode链接:https://leetcode.com/problems/minimum-time-visiting-all-points/
    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.
    *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.

    public int minTimeToVisitAllPoints(int[][] points) {
          int t = 0;
          for(int i = 0;i <points.length-1;i++) {
                t += Math.max(Math.abs(points[i][0]-points[i+1][0]),Math.abs(points[i][1]-points[i+1][1]));
          }
          return t;
    }
    

    小结:找到规律,并使用Math的静态方法max、abs;

    相关文章

      网友评论

          本文标题:1266. Minimum Time Visiting All

          本文链接:https://www.haomeiwen.com/subject/apvwrhtx.html