Skip to content

Latest commit

 

History

History
93 lines (63 loc) · 4.41 KB

197.md

File metadata and controls

93 lines (63 loc) · 4.41 KB

Difficulty: 🟢 Easy

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 vertically by one unit,
    • move horizontally by one unit, or
    • move diagonally sqrt(2) units (in other words, move one unit vertically then one unit horizontally in 1 second).
  • You have to visit the points in the same order as they appear in the array.
  • You are allowed to pass through points that appear later in the order, but these do not count as visits.

Examples:

Example 1:

197_01.png

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]
Time from [1,1] to [3,4] = 3 seconds
Time from [3,4] to [-1,0] = 4 seconds
Total time = 7 seconds

Example 2:

Input: points = [[3,2],[-2,2]]
Output: 5

Constraints:

  • points.length == n
  • 1 <= n <= 100
  • points[i].length == 2
  • 1000 <= points[i][0], points[i][1] <= 1000

Solutions

O(n) solution

Python3

class Solution:
    def minTimeToVisitAllPoints(self, points: List[List[int]]) -> int:
        count = len(points)
        min_time = 0

        for index in range(1, count):
            x0, y0 = points[index-1]
            x1, y1 = points[index]
            digonally_travel_time = min(abs(x1-x0), abs(y1-y0))
            vertical_or_horizontal_travel_time = max(abs(x1-x0), abs(y1-y0)) - digonally_travel_time
            min_time += digonally_travel_time + vertical_or_horizontal_travel_time
        
        return min_time

The solution iterates through the list of points in order, starting from the second point and moving to subsequent points. For each pair of consecutive points (points[i-1] and points[i]), it calculates the minimum time required to move from the first point to the second point using the allowed movements: vertical, horizontal, or diagonal.

Here's how the solution works:

  1. Initialize variables min_time to 0 and count to the number of points.
  2. Iterate through the points starting from the second point (index 1) to the last point (index count-1). For each pair of consecutive points (points[i-1] and points[i]), do the following:
    • Calculate the absolute differences between the x-coordinates and y-coordinates of the two points: dx = abs(x1 - x0) and dy = abs(y1 - y0).
    • Calculate the minimum time required to move diagonally from the first point to the second point. This is equal to the minimum of dx and dy, as each diagonal move covers √2 units. Store this value in diagonal_travel_time.
    • Calculate the time required to move either vertically or horizontally from the first point to the second point. This is equal to the maximum of dx and dy minus the diagonal travel time. Store this value in vertical_or_horizontal_travel_time.
    • Add diagonal_travel_time and vertical_or_horizontal_travel_time together and increment min_time by this sum. This represents the minimum time required to move from the first point to the second point.
  3. After iterating through all points, the min_time variable contains the minimum time required to visit all the points in the given order.
  4. Return the value of min_time as the final result.

Complexity Analysis

  • Time Complexity: The solution iterates through the list of points once, performing constant-time calculations for each pair of consecutive points. Therefore, the time complexity is O(n), where n is the number of points.
  • Space Complexity: The solution uses a constant amount of additional space to store variables, so the space complexity is O(1).

Summary

The provided solution effectively calculates the minimum time required to visit a series of points on a 2D plane following specific movement rules. It iterates through the points, calculates the travel time for each pair of consecutive points, and accumulates the total minimum travel time. The solution has a time complexity of O(n) and a space complexity of O(1), making it efficient for the given constraints.

NB: If you want to get community points please suggest solutions in other languages as merge requests.