File: minimum-time-visiting-all-points/solution.py

Date: 2026-06-06

Time: 18:03

minimum-time-visiting-all-points/solution.py

Purpose

Solves LeetCode 1266 — Minimum Time Visiting All Points. The file owns a single function that computes the minimum time (in seconds) to visit an ordered sequence of 2D points, where each second you can move one step in any of 8 directions (including diagonals).

Key Components

minTimeToVisitAllPoints(points: list[list[int]]) -> int — The sole public function. Takes an ordered list of [x, y] coordinate pairs and returns the total minimum traversal time.

Contract: points must contain at least one element. The function visits points in the given order (no reordering optimization).

Patterns

Chebyshev distance. The core insight is that the minimum time to travel between two points on an infinite 2D grid with 8-directional movement equals the Chebyshev distance: max(|dx|, |dy|). Diagonal moves let you close both the x-gap and y-gap simultaneously, so the bottleneck is whichever axis has the larger delta.

Pairwise reduction. The function sums Chebyshev distances over consecutive pairs using a generator expression with index arithmetic (points[i+1] vs points[i]), rather than zip or itertools.pairwise. This is a common LeetCode idiom — functional but slightly less Pythonic than sum(max(...) for a, b in pairwise(points)).

Dependencies

Imports: None — pure computation with no standard library or third-party dependencies.

Imported by: minimum-time-visiting-all-points/test_solution.py (directly), plus ~400+ other test files listed in the repo context. That "imported by" list is likely an artifact of the repo's test harness importing a shared runner or conftest, not actual usage of this function.

Flow

1. Iterate i from 0 to len(points) - 2.

2. For each consecutive pair (points[i], points[i+1]), compute the absolute x-difference and absolute y-difference.

3. Take the max of those two deltas — this is the Chebyshev distance.

4. Sum all pairwise distances and return.

Single-pass, O(n) time, O(1) space.

Invariants

Error Handling

None. An empty points list produces 0 (vacuous sum). Invalid inputs (non-numeric, wrong nesting) would raise at the abs() or indexing level with standard Python exceptions — no guarding is done, which is typical for LeetCode solutions.

Topics to Explore

Beliefs