Given an array of points where points[i] = [xi, yi] represents a point on the 2D plane, return the minimum time in seconds to visit all the points in the order given by points.
You can move one unit distance in either the x-direction, y-direction, or diagonally (in both x and y direction).
points = [[1,1],[3,4],[-1,0]]
Output: 7
Explanation:
points = [[3,2],[-2,2]]
Output: 5
Explanation:
points.length >= 1points[i].length == 2-1000 <= xi, yi <= 1000points.length >= 1 as per the constraints.(xi, yi) to (xf, yf), the minimum time would be max(abs(xf - xi), abs(yf - yi)).def minTimeToVisitAllPoints(points):
total_time = 0
for i in range(1, len(points)):
x0, y0 = points[i - 1]
x1, y1 = points[i]
time_to_next_point = max(abs(x1 - x0), abs(y1 - y0))
total_time += time_to_next_point
return total_time
# Test cases
print(minTimeToVisitAllPoints([[1, 1], [3, 4], [-1, 0]])) # Output: 7
print(minTimeToVisitAllPoints([[3, 2], [-2, 2]])) # Output: 5
The time complexity for this solution is O(n), where n is the number of points in the input list. This is because we iterate through the points exactly once.
The space complexity is O(1) since we are using a fixed amount of extra space regardless of the input size.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?