forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1266.java
23 lines (21 loc) · 785 Bytes
/
_1266.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.fishercoder.solutions;
public class _1266 {
public static class Solution1 {
/**
* Time: O(n)
* Space: O(1)
* <p>
* credit: https://leetcode.com/problems/minimum-time-visiting-all-points/discuss/436142/Sum-of-Chebyshev-distance-between-two-consecutive-points
*/
public int minTimeToVisitAllPoints(int[][] points) {
int minTime = 0;
for (int i = 0; i < points.length - 1; i++) {
minTime += chebyshevDistance(points[i], points[i + 1]);
}
return minTime;
}
private int chebyshevDistance(int[] pointA, int[] pointB) {
return Math.max(Math.abs(pointA[0] - pointB[0]), Math.abs(pointA[1] - pointB[1]));
}
}
}