Minimum Time Visiting All Points - Problem

On a 2D plane, there are n points with integer coordinates points[i] = [xi, yi]. Return the minimum time in seconds to visit all the points in the order given by points.

You can move according to these rules:

  • In 1 second, you can either:
  • Move vertically by one unit
  • Move horizontally by one unit
  • Move diagonally √2 units (in other words, move one unit vertically then one unit horizontally in 1 second)

You have to visit the points in the same order as they appear in the array. You are allowed to pass through points that appear later in the order, but these do not count as visits.

Input & Output

Example 1 — Basic Case
$ Input: points = [[1,1],[3,4],[-1,0]]
Output: 7
💡 Note: From [1,1] to [3,4]: max(|3-1|, |4-1|) = max(2,3) = 3 seconds. From [3,4] to [-1,0]: max(|-1-3|, |0-4|) = max(4,4) = 4 seconds. Total: 3 + 4 = 7 seconds.
Example 2 — Straight Line Movement
$ Input: points = [[3,2],[-2,2]]
Output: 5
💡 Note: From [3,2] to [-2,2]: max(|-2-3|, |2-2|) = max(5,0) = 5 seconds. Only horizontal movement needed.
Example 3 — Single Point
$ Input: points = [[0,0]]
Output: 0
💡 Note: Only one point, no movement required. Time = 0 seconds.

Constraints

  • 1 ≤ points.length ≤ 100
  • points[i].length == 2
  • -1000 ≤ points[i][0], points[i][1] ≤ 1000

Visualization

Tap to expand
Minimum Time Visiting All Points INPUT x y (1,1) P1 (3,4) P2 (-1,0) P3 Input Array: points = [[1,1],[3,4],[-1,0]] Visit order: (1,1) (3,4) (-1,0) n = 3 points ALGORITHM STEPS 1 Chebyshev Distance max(|dx|, |dy|) for each pair 2 P1 to P2 Distance dx=|3-1|=2, dy=|4-1|=3 time = max(2,3) = 3 3 P2 to P3 Distance dx=|-1-3|=4, dy=|0-4|=4 time = max(4,4) = 4 4 Sum All Times Total = 3 + 4 = 7 seconds Chebyshev Formula: d = max(|x2-x1|, |y2-y1|) Diagonal moves save time! 1 diagonal = 1 second FINAL RESULT Complete Path: START (1,1) --> 3s P2 (3,4) --> 4s END (-1,0) (1,1) --> (3,4): 3 seconds (3,4) --> (-1,0): 4 seconds OUTPUT 7 Minimum Time: 7 seconds OK - All points visited! Key Insight: Chebyshev Distance Since diagonal moves take 1 second (same as horizontal/vertical), the minimum time between two points is max(|dx|, |dy|). Move diagonally as much as possible, then straight for the remainder. Time Complexity: O(n) | Space Complexity: O(1) - One pass through all point pairs. TutorialsPoint - Minimum Time Visiting All Points | One-Pass Chebyshev Distance Approach
Asked in
Amazon 12 Google 8
28.5K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen