Learnitweb

LeetCode Problem 1266: Minimum Time Visiting All Points

Problem Overview

You are given an array points where points[i] = [xᵢ, yᵢ] represents a point on a 2D plane.
You start at the first point and must visit all points sequentially (in the order they appear).

In one second, you can:

  • Move one unit horizontally, vertically, or diagonally (in one move, both x and y can change by ±1).

You need to return the minimum time (in seconds) required to visit all the given points.

Example 1:

Input: points = [[1,1],[3,4],[-1,0]]
Output: 7
Explanation:
From (1,1) to (3,4) → takes max(|3-1|, |4-1|) = 3 seconds
From (3,4) to (-1,0) → takes max(|-1-3|, |0-4|) = 4 seconds
Total = 3 + 4 = 7 seconds

Example 2:

Input: points = [[3,2],[-2,2]]
Output: 5
Explanation:
From (3,2) to (-2,2) → horizontal move only, 5 seconds

Intuition / Approach

When moving between two points (x₁, y₁) and (x₂, y₂):

  • In one move, you can increase/decrease both x and y by 1 (diagonal move).
  • So, you can move diagonally until one of the coordinates aligns, and then move straight horizontally or vertically.

Thus, the minimum time to move from one point to another is determined by:

max(|x₂ - x₁|, |y₂ - y₁|)

Because:

  • If |x₂ - x₁| > |y₂ - y₁|, after moving diagonally |y₂ - y₁| times, you’ll still need extra horizontal moves.
  • If |y₂ - y₁| > |x₂ - x₁|, you’ll still need extra vertical moves.

Therefore, the total time to visit all points sequentially is the sum of these maximum distances between consecutive points.


Step-by-Step Algorithm Explanation

  1. Initialize totalTime = 0.
  2. Loop from the first point to the second-last point:
    • For each pair (x₁, y₁) and (x₂, y₂):
      • Calculate the x-distance: dx = Math.abs(x₂ - x₁)
      • Calculate the y-distance: dy = Math.abs(y₂ - y₁)
      • The time between these two points = max(dx, dy)
    • Add this time to totalTime.
  3. Return totalTime as the final result.

Example Walkthrough

Let’s walk through the example:

Input: points = [[1,1],[3,4],[-1,0]]

StepFromTodxdymax(dx,dy)Time So Far
1(1,1)(3,4)2333
2(3,4)(-1,0)4447

Output: 7


Java Code Implementation

public class Solution {
    public int minTimeToVisitAllPoints(int[][] points) {
        int totalTime = 0;
        
        for (int i = 0; i < points.length - 1; i++) {
            int x1 = points[i][0];
            int y1 = points[i][1];
            int x2 = points[i + 1][0];
            int y2 = points[i + 1][1];
            
            int dx = Math.abs(x2 - x1);
            int dy = Math.abs(y2 - y1);
            
            // The time between two points is determined by the maximum of dx and dy
            totalTime += Math.max(dx, dy);
        }
        
        return totalTime;
    }
}

Complexity Analysis

  1. Time Complexity:
    • We iterate once through all pairs of consecutive points.
    • Therefore, time complexity = O(n), where n = number of points.
  2. Space Complexity:
    • We only use a few integer variables for calculations.
    • Therefore, space complexity = O(1).

Alternative Explanation

Instead of simulating each step, note that:

  • You can always cover both x and y movements simultaneously using diagonal moves until one coordinate matches.
  • After that, only one axis movement remains.
  • Hence, the maximum of dx and dy always represents the minimal required steps.

This observation directly simplifies the problem without loops or complex logic.