Problem Description
Explanation
To find the minimum time to visit all points in the given order, we can calculate the time taken to move from one point to another by considering the maximum of the absolute differences between the x and y coordinates of consecutive points. This is because we can move diagonally whenever we have to make both x and y coordinate changes.
The time taken to move from one point to another is given by the maximum of the absolute differences in their x and y coordinates. Hence, the total time taken will be the sum of these maximum differences for each pair of consecutive points.
The time complexity of this approach is O(n) where n is the number of points, and the space complexity is O(1) as we are not using any extra space.
Solutions
class Solution {
public int minTimeToVisitAllPoints(int[][] points) {
int minTime = 0;
for (int i = 1; i < points.length; i++) {
int xDiff = Math.abs(points[i][0] - points[i - 1][0]);
int yDiff = Math.abs(points[i][1] - points[i - 1][1]);
minTime += Math.max(xDiff, yDiff);
}
return minTime;
}
}
Loading editor...