1266. 访问所有点的最小时间
-
自己画出几个例子就能发现规律,当 (abs(x1-x2)=abs(y1-y2)) 的长度相等时,那么最小的距离就是 (abs(x1-x2)),
否则就是两边那个较大的。
class Solution {
public:
int minTimeToVisitAllPoints(vector<vector<int>>& points) {
if(points.size()==1)
return 0;
int ans=0;
//cout<<floor(4.9)<<endl;
for(int i=0;i<points.size()-1;i++)
{
//double cnt=sqrt((points[i][0]-points[i+1][0])*(points[i][0]-points[i+1][0])+(points[i][1]-points[i+1][1])*(points[i][1]-points[i+1][1]));
// cout<<"cnt="<<cnt<<endl;
if(abs(points[i][0]-points[i+1][0])==abs(points[i][1]-points[i+1][1]))
{
ans+=abs(points[i][0]-points[i+1][0]);
//cout<<"haha"<<endl;cout<<abs(points[i][0]-points[i+1][0])<<endl;
}
else
ans+=max(abs(points[i][0]-points[i+1][0]),abs(points[i][1]-points[i+1][1]));
}
//cout<<"ans="<<ans<<endl;
return ans;
}
};