-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray1266.java
More file actions
57 lines (56 loc) · 1.69 KB
/
Array1266.java
File metadata and controls
57 lines (56 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package array;
/**
* @ProjectName: leetcode
* @Package: array
* @ClassName: Array1266
* @Author: markey
* @Description:
* 平面上有 n 个点,点的位置用整数坐标表示 points[i] = [xi, yi]。请你计算访问所有这些点需要的最小时间(以秒为单位)。
*
* 你可以按照下面的规则在平面上移动:
*
* 每一秒沿水平或者竖直方向移动一个单位长度,或者跨过对角线(可以看作在一秒内向水平和竖直方向各移动一个单位长度)。
* 必须按照数组中出现的顺序来访问这些点。
*
*
* 示例 1:
*
*
*
* 输入:points = [[1,1],[3,4],[-1,0]]
* 输出:7
* 解释:一条最佳的访问路径是: [1,1] -> [2,2] -> [3,3] -> [3,4] -> [2,3] -> [1,2] -> [0,1] -> [-1,0]
* 从 [1,1] 到 [3,4] 需要 3 秒
* 从 [3,4] 到 [-1,0] 需要 4 秒
* 一共需要 7 秒
* 示例 2:
*
* 输入:points = [[3,2],[-2,2]]
* 输出:5
*
*
* 提示:
*
* points.length == n
* 1 <= n <= 100
* points[i].length == 2
* -1000 <= points[i][0], points[i][1] <= 1000
*
* 来源:力扣(LeetCode)
* 链接:https://leetcode-cn.com/problems/minimum-time-visiting-all-points
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
* @Date: 2020/1/5 10:28
* @Version: 1.0
*/
public class Array1266 {
public int minTimeToVisitAllPoints(int[][] points) {
int res = 0;
for (int i = 1; i < points.length; i++) {
res += timeFromAToB(points[i], points[i-1]);
}
return res;
}
private int timeFromAToB(int[] a, int [] b) {
return Math.max(Math.abs(a[0] - b[0]) , Math.abs(a[1] - b[1]));
}
}