Minimum Time Visiting All Points
01 · Problem
You are given n points on a 2D grid as points[i] = [x_i, y_i]. You start at points[0] and must visit the points in the given order. In one second you can move by one unit horizontally, one unit vertically, or one unit diagonally (one unit in both x and y at the same time).
Return the minimum number of seconds needed to visit every point in order. Passing over a later point early does not count as visiting it; only the given order matters. If there is a single point, the answer is 0.
02 · Examples
points = [[1,1],[3,4],[-1,0]]
7
From [1,1] to [3,4]: dx = 2, dy = 3, so 3 seconds. From [3,4] to [-1,0]: dx = 4, dy = 4, so 4 seconds. Total 7.
points = [[0,0],[2,5]]
5
Move diagonally twice to [2,2], then up three times to [2,5]: max(2,5) = 5 seconds.
points = [[3,2],[-2,2]]
5
The points share a y-coordinate, so the trip is 5 horizontal moves.
03 · Constraints
- 011 <= points.length <= 100
- 02points[i].length == 2
- 03-1000 <= x_i, y_i <= 1000
04 · Optimal complexity
- Time
- O(n)
- Space
- O(1)
Practice it alone or rehearse it as an interview.
Practice Mode gives you an editor and test runs, nothing else. AI Interview Mode puts a voice interviewer on the other side, adds a clock, and ends with a scored summary of the round.