반응형
https://leetcode.com/problems/minimum-time-visiting-all-points/description
현 좌표와 전 좌표의 x 변화량, y 변화량을 구해줍니다.
대각선 이동, 수평 또는 수직 이동 모두 동일하게 1초 걸리기 때문에, 더 큰 변화량이 최소 이동 시간이 됩니다.
class Solution {
public:
int minTimeToVisitAllPoints(vector<vector<int>>& points) {
int res = 0;
for(int i=1; i<points.size(); i++) {
int px = points[i - 1][0];
int py = points[i - 1][1];
int cx = points[i][0];
int cy = points[i][1];
int dx = abs(px - cx);
int dy = abs(py - cy);
res += max(dx, dy);
}
return res;
}
};
반응형
'Algorithm' 카테고리의 다른 글
LeetCode 1688. Count of Matches in Tournament (0) | 2023.12.06 |
---|---|
LeetCode 2264. Largest 3-Same-Digit Number in String (0) | 2023.12.04 |
LeetCode 1160. Find Words That Can Be Formed by Characters (2) | 2023.12.02 |
LeetCode 1662. Check If Two String Arrays are Equivalent (0) | 2023.12.02 |
LeetCode 2147. Number of Ways to Divide a Long Corridor (1) | 2023.11.29 |