Minimum Time Visiting All Points (LeetCode 1266): Solution and Explanation

Easy
Array
Math
Geometry

You get a list of integer points on a grid and must visit them in order. Each second you can step one unit horizontally, vertically or diagonally. Return the fewest seconds the whole tour takes.

Intuition

Because the order is fixed, the tour splits into independent legs: point 0 to point 1, point 1 to point 2, and so on. The best tour is just the best time for each leg added together, so the whole problem reduces to one question about two points.

For one leg, let dx be the horizontal gap and dy the vertical gap. A diagonal step shrinks both gaps by one for the price of a single second, while a straight step shrinks only one of them. So you should move diagonally as long as both gaps are open, which is min(dx, dy) steps, and then walk straight along the remaining |dx - dy|.

Those two parts add up to max(dx, dy). You cannot do better, because every second reduces the larger gap by at most one. That makes max(dx, dy) both achievable and a lower bound, so it is exactly the cost of the leg. This distance has a name: the Chebyshev distance, the number of moves a chess king needs.

Approach: Sum of Chebyshev distances

  1. Set time = 0.
  2. For every i from 1 to n - 1, compute dx = |points[i][0] - points[i-1][0]| and dy = |points[i][1] - points[i-1][1]|.
  3. Add max(dx, dy) to time, since the leg takes min(dx, dy) diagonal steps and |dx - dy| straight steps.
  4. Return time. With a single point the loop never runs and the answer is 0.

Step-by-step example

points = [[1,1],[3,4],[-1,0]]
  1. Start with time = 0.
  2. Leg 0 to 1, from (1,1) to (3,4): dx = 2, dy = 3. Two diagonal steps reach (3,3), one straight step reaches (3,4). The leg costs max(2, 3) = 3, so time = 3.
  3. Leg 1 to 2, from (3,4) to (-1,0): dx = 4, dy = 4. Four diagonal steps go straight there with no straight part. The leg costs 4, so time = 7.
  4. No legs remain, so the function returns 7.

Code: Python, Java, C++

1class Solution:
2    def minTimeToVisitAllPoints(self, points: List[List[int]]) -> int:
3        time = 0
4        for i in range(1, len(points)):
5            dx = abs(points[i][0] - points[i - 1][0])
6            dy = abs(points[i][1] - points[i - 1][1])
7            time += max(dx, dy)
8        return time
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

Each consecutive pair is looked at once and costs constant work.

Space

O(1)

Only the running total and the two gaps are stored.

Other approaches

Step-by-step simulation (time O(n * D), space O(1))

Walk a cursor toward each target, moving diagonally while both coordinates differ and straight otherwise, and count the moves. It gives the same answer and makes the greedy visible, but it costs time proportional to the distances (up to 2000 per leg) instead of constant time.

Common mistakes

  • Adding dx + dy (the Manhattan distance). That ignores diagonal moves and overcounts whenever both gaps are positive.
  • Forgetting the absolute values, so a leg that moves left or down produces a negative or too small cost.
  • Using the straight-line Euclidean distance. Diagonal steps cost one second, not sqrt(2) seconds, so the geometry of the grid matters, not the length of the line.
  • Trying to reorder the points for a shorter tour. The problem fixes the order, so each leg is independent.

Frequently asked questions

Why is the cost of one leg max(dx, dy)?

Diagonal steps close both gaps together for min(dx, dy) seconds, and straight steps finish the leftover |dx - dy|. The sum is max(dx, dy), and no path can be shorter because each second shrinks the larger gap by at most one.

Does passing through a later point early help?

No. Passing through it does not count as visiting it, and each leg is already as short as possible, so the total is simply the sum of the leg costs.

What if two consecutive points are the same?

Both gaps are 0, so the leg costs 0 seconds and the formula handles it without a special case.

What are the time and space complexity?

O(n) time for one pass over the consecutive pairs and O(1) extra space.