资讯动态

LeetCode //C - 1266. Minimum Time Visiting All Points

发布时间:2026/9/27 23:50:35 来源:尧图企业网站定制
1266. Minimum Time Visiting All PointsOn a 2D plane, there are n points with integer coordinatesp o i n t s [ i ] [ x i , y i ] points[i] [x_i, y_i]points[i][xi​,yi​]. Returnthe minimum time in seconds to visit all the points in the order given by points.You can move according to these rules:In 1 second, you can either:move vertically by one unit,move horizontally by one unit, ormove diagonally sqrt(2) units (in other words, move one unit vertically then one unit horizontally in 1 second).You have to visit the points in the same order as they appear in the array.You are allowed to pass through points that appear later in the order, but these do not count as visits.Example 1:Input:points [[1,1],[3,4],[-1,0]]Output:7Explanation:One optimal path is [1,1] - [2,2] - [3,3] - [3,4] - [2,3] - [1,2] - [0,1] - [-1,0]Time from [1,1] to [3,4] 3 secondsTime from [3,4] to [-1,0] 4 secondsTotal time 7 secondsExample 2:Input:points [[3,2],[-2,2]]Output:5Constraints:points.length n1 n 100points[i].length 2-1000 points[i][0], points[i][1] 1000From: LeetCodeLink: 1266. Minimum Time Visiting All PointsSolution:Ideas:between two points, use diagonal moves as much as possible, so time is max(abs(dx), abs(dy)).Code:intminTimeToVisitAllPoints(int**points,intpointsSize,int*pointsColSize){inttime0;for(inti1;ipointsSize;i){intdxpoints[i][0]-points[i-1][0];intdypoints[i][1]-points[i-1][1];if(dx0)dx-dx;if(dy0)dy-dy;timedxdy?dx:dy;}returntime;}

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价 →
↑