Minimum Time Visiting All Points — LeetCode 1266 Python Solution

EasyGeometryArrayMath
Problem
#1266
Reading time
2 min

The 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.

Example

Input
points = [[1,1],[3,4],[-1,0]]
Output
7
Explanation
One optimal path is [1,1] -> [2,2] -> [3,3] -> [3,4] -> [2,3] -> [1,2] -> [0,1] -> [-1,0]

Python solution

Python
class Solution:
    def minTimeToVisitAllPoints(self, points: List[List[int]]) -> int:
        return sum(
            max(abs(p1[0] - p2[0]), abs(p1[1] - p2[1])) for p1, p2 in pairwise(points)
        )

Complexity

MeasureComplexity
TimeO(n), where n is the number of points
SpaceO(1) auxiliary

Pattern: Math and Number Theory

Find the closed form, the invariant, or the modular identity — and skip the loop entirely. LeetCode 1266. Minimum Time Visiting All Points is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Math and Geometry.

The math and number theory guide has the Python template for the pattern and the 485 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 1266. Minimum Time Visiting All Points?
LeetCode 1266. Minimum Time Visiting All Points is rated Easy on LeetCode.
What is the time complexity of LeetCode 1266. Minimum Time Visiting All Points?
The Python solution on this page runs in O(n), where n is the number of points.
What is the space complexity of LeetCode 1266. Minimum Time Visiting All Points?
The Python solution on this page uses O(1) auxiliary space.
What topics does LeetCode 1266. Minimum Time Visiting All Points cover?
LeetCode 1266. Minimum Time Visiting All Points is tagged Geometry, Array and Math on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview