266. 访问所有点的最小时间:
平面上有 n 个点,点的位置用整数坐标表示 points[i] = [, ] 。请你计算访问所有这些点需要的 最小时间(以秒为单位)。
你需要按照下面的规则在平面上移动:
- 每一秒内,你可以:
- 沿水平方向移动一个单位长度,或者
- 沿竖直方向移动一个单位长度,或者
- 跨过对角线移动
sqrt(2)个单位长度(可以看作在一秒内向水平和竖直方向各移动一个单位长度)。
- 必须按照数组中出现的顺序来访问这些点。
- 在访问某个点时,可以经过该点后面出现的点,但经过的那些点不算作有效访问。
样例 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
分析
- 面对这道算法题目,二当家的陷入了沉思。
- 题目要求必须按顺序访问,途径的点不算有效访问,其实这样反而简单,也就是只用关心相邻的两个点。
- 我们只需要考虑相邻的两个点的之间的移动需要的最小时间(如果x和y轴都有距离,我们就斜着移动,否则就垂直或水平移动,所以我们只需要考虑x和y轴之间相对较大的距离。)。
题解
java
class Solution {
public int minTimeToVisitAllPoints(int[][] points) {
int ans = 0;
for (int i = 1; i < points.length; ++i) {
int xd = Math.abs(points[i][0] - points[i - 1][0]);
int yd = Math.abs(points[i][1] - points[i - 1][1]);
ans += Math.max(xd, yd);
}
return ans;
}
}
c
int minTimeToVisitAllPoints(int** points, int pointsSize, int* pointsColSize){
int ans = 0;
for (int i = 1; i < pointsSize; ++i) {
int xd = points[i][0] - points[i - 1][0];
xd = xd < 0 ? -xd : xd;
int yd = points[i][1] - points[i - 1][1];
yd = yd < 0 ? -yd : yd;
ans += xd > yd ? xd : yd;
}
return ans;
}
c++
class Solution {
public:
int minTimeToVisitAllPoints(vector<vector<int>>& points) {
int ans = 0;
for (int i = 1; i < points.size(); ++i) {
int xd = abs(points[i][0] - points[i - 1][0]);
int yd = abs(points[i][1] - points[i - 1][1]);
ans += max(xd, yd);
}
return ans;
}
};
python
class Solution:
def minTimeToVisitAllPoints(self, points: List[List[int]]) -> int:
ans = 0
for i in range(1, len(points)):
xd = abs(points[i][0] - points[i - 1][0])
yd = abs(points[i][1] - points[i - 1][1])
ans += max(xd, yd)
return ans
go
func minTimeToVisitAllPoints(points [][]int) int {
ans := 0
for i := 1; i < len(points); i++ {
xd := math.Abs(float64(points[i][0] - points[i - 1][0]))
yd := math.Abs(float64(points[i][1] - points[i - 1][1]))
ans += int(math.Max(xd, yd))
}
return ans
}
rust
impl Solution {
pub fn min_time_to_visit_all_points(points: Vec<Vec<i32>>) -> i32 {
let mut ans = 0;
(1..points.len()).for_each(|i|{
let xd = (points[i][0] - points[i - 1][0]).abs();
let yd = (points[i][1] - points[i - 1][1]).abs();
ans += xd.max(yd);
});
ans
}
}
javascript
/**
* @param {number[][]} points
* @return {number}
*/
var minTimeToVisitAllPoints = function(points) {
ans = 0;
for (let i = 1; i < points.length; i++) {
xd = Math.abs(points[i][0] - points[i - 1][0]);
yd = Math.abs(points[i][1] - points[i - 1][1]);
ans += Math.max(xd, yd);
}
return ans;
};
typescript
function minTimeToVisitAllPoints(points: number[][]): number {
let ans = 0;
for (let i = 1; i < points.length; i++) {
let xd = Math.abs(points[i][0] - points[i - 1][0]);
let yd = Math.abs(points[i][1] - points[i - 1][1]);
ans += Math.max(xd, yd);
}
return ans;
};
原题传送门:https://leetcode-cn.com/problems/minimum-time-visiting-all-points/
非常感谢你阅读本文~
放弃不难,但坚持一定很酷~
希望我们大家都能每天进步一点点~
本文由 二当家的白帽子:https://juejin.cn/user/2771185768884824/posts 博客原创~
我正在参与掘金技术社区创作者签约计划招募活动,点击链接报名投稿。