Skip to content

1266. Minimum Time Visiting All Points 👍

  • Time:
  • Space:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution {
 public:
  int minTimeToVisitAllPoints(vector<vector<int>>& points) {
    int ans = 0;

    for (int i = 1; i < points.size(); ++i)
      ans += max(abs(points[i][0] - points[i - 1][0]),
                 abs(points[i][1] - points[i - 1][1]));

    return ans;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
  public int minTimeToVisitAllPoints(int[][] points) {
    int ans = 0;

    for (int i = 1; i < points.length; ++i)
      ans += Math.max(Math.abs(points[i][0] - points[i - 1][0]),
                      Math.abs(points[i][1] - points[i - 1][1]));

    return ans;
  }
}
1
2
3
4
5
6
7
8
9
class Solution:
  def minTimeToVisitAllPoints(self, points: List[List[int]]) -> int:
    ans = 0

    for i in range(1, len(points)):
      ans += max(abs(points[i][0] - points[i - 1][0]),
                 abs(points[i][1] - points[i - 1][1]))

    return ans
Back to top