Minimum Time Visiting All Points - Problem

Imagine you're a robot navigator on a 2D coordinate plane with n points marked at integer coordinates points[i] = [xi, yi]. Your mission is to visit all points in the exact order given in the array, and you need to calculate the minimum time in seconds required to complete this journey.

You have three movement options, each taking exactly 1 second:

  • ๐Ÿ”„ Move horizontally by one unit
  • ๐Ÿ”„ Move vertically by one unit
  • โ†—๏ธ Move diagonally by one unit (equivalent to moving one unit both horizontally and vertically simultaneously)

The key insight is that diagonal movement covers the maximum distance in minimum time! You can pass through future points during your journey, but they don't count as visits until you reach them in order.

Goal: Return the minimum total time to visit all points sequentially.

Input & Output

example_1.py โ€” Basic Path
$ 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.py โ€” Single Point
$ Input: points = [[3,2]]
โ€บ Output: 0
๐Ÿ’ก Note: Only one point to visit, no movement required, so time = 0 seconds.
example_3.py โ€” Aligned Points
$ Input: points = [[0,0],[1,1],[2,2]]
โ€บ Output: 2
๐Ÿ’ก Note: From [0,0] to [1,1]: max(1,1) = 1. From [1,1] to [2,2]: max(1,1) = 1. Total: 1 + 1 = 2 seconds. Pure diagonal movement.

Visualization

Tap to expand
StartEnddx = 6dy = 3Diagonal: 3 movesStraight: 3 movesOptimal Time:max(6,3) = 6โ™” King movesefficiently!Chess King's Optimal Path
Understanding the Visualization
1
Analyze Distance
Calculate horizontal (dx) and vertical (dy) distances
2
Move Diagonally
Use diagonal moves to cover min(dx, dy) distance
3
Finish Straight
Complete remaining distance with straight moves
4
Sum All Segments
Add up times for all consecutive point pairs
Key Takeaway
๐ŸŽฏ Key Insight: The Chebyshev distance formula max(dx, dy) gives us the minimum time because diagonal movement is the most efficient way to cover distance in both dimensions simultaneously.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through all points, constant time per pair

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using variables to track current calculation

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค points.length โ‰ค 100
  • points[i].length == 2
  • -1000 โ‰ค points[i][0], points[i][1] โ‰ค 1000
Asked in
Google 15 Amazon 12 Microsoft 8 Apple 6
48.0K Views
Medium Frequency
~8 min Avg. Time
2.2K 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