平面上有 n 個(gè)點(diǎn),點(diǎn)的位置用整數(shù)坐標(biāo)表示 points[i] = [xi, yi]。請(qǐng)你計(jì)算訪問所有這些點(diǎn)需要的最小時(shí)間(以秒為單位)。
你可以按照下面的規(guī)則在平面上移動(dòng):
每一秒沿水平或者豎直方向移動(dòng)一個(gè)單位長(zhǎng)度,或者跨過對(duì)角線(可以看作在一秒內(nèi)向水平和豎直方向各移動(dòng)一個(gè)單位長(zhǎng)度)。
必須按照數(shù)組中出現(xiàn)的順序來訪問這些點(diǎn)
找到兩個(gè)點(diǎn)的x坐標(biāo)和y坐標(biāo),看誰離的遠(yuǎn),離的遠(yuǎn)的就是這兩個(gè)點(diǎn)需要幾步,我們只需要把所有點(diǎn)需要走的步數(shù)加起來就行了。
輸入: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
來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/minimum-time-visiting-all-points
著作權(quán)歸領(lǐng)扣網(wǎng)絡(luò)所有。商業(yè)轉(zhuǎn)載請(qǐng)聯(lián)系官方授權(quán),非商業(yè)轉(zhuǎn)載請(qǐng)注明出處。
int abs(int x){
if(x>=0)
return x;
else
return -x;
}
int max(int x,int y){
if(x>=y)
return x;
else return y;
}
int minTimeToVisitAllPoints(int** points, int pointsSize, int* pointsColSize){
if(pointsSize == 0)
return 0;
int sum = 0;
for(int i=0;i<pointsSize-1;i++){
int * pointsColSize1 = points[i];
int * pointsColSize2 = points[i+1];
sum+=max(abs(pointsColSize1[0]-pointsColSize2[0]),abs(pointsColSize1[1]-pointsColSize2[1]));
}
return sum;
}