1266. Minimum Time Visiting All Points
T: O(n)
S: O(1)
```java
class Solution {
public int minTimeToVisitAllPoints(int[][] points) {
int result = 0;
for (int i = 0; i < points.length-1; i++) {
int XCurr = points[i][0];
int XNext = points[i+1][0];
int YCurr = points[i][1];
int YNext = points[i+1][1];
result += Math.max(Math.abs(XCurr - XNext), Math.abs(YCurr - YNext));
}
return result;
}
}
```
```java
/**
why min dist is max(diffX, diffY)?
because when we move x -> actually we also move y (by diagnal way)
so if we move x, but still can't arrive, the left is diffY - diffX) (this is positive number)
so we only need to know which is bigger -> so it's max(diffX, diffY)
key point is it can move by "diagnal way"
ex: [1,1],[3,4]
4 3,4
3 x -> 2 units (x can diagnal walk 2 units), then just vertically move to 3,4 by 1 units
2 x
1 1,1
1 2 3
max(abs(3-1), abs(4-1)) = max(2, 3) = 3
*/
```
Last updated