Created
January 12, 2026 19:41
-
-
Save tatsuyax25/bb31d8d8c91402d52587da138191ec1f to your computer and use it in GitHub Desktop.
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 verti
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {number[][]} points | |
| * @return {number} | |
| */ | |
| var minTimeToVisitAllPoints = function(points) { | |
| // Total time accumulator | |
| let totalTime = 0; | |
| // Iterate through each consecutive pair of points | |
| for (let i = 1; i < points.length; i++) { | |
| const [x1, y1] = points[i - 1]; | |
| const [x2, y2] = points[i]; | |
| // Compute horizontal and vertical distances | |
| const dx = Math.abs(x2 - x1); | |
| const dy = Math.abs(y2 - y1); | |
| // The key insight: | |
| // - You can move diagonally min(dx, dy) times. | |
| // - Then you move straight for the remaining difference. | |
| // - Total time = max(dx, dy) | |
| totalTime += Math.max(dx, dy); | |
| } | |
| return totalTime; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment