반응형

https://leetcode.com/problems/minimum-time-visiting-all-points/description

 

Minimum Time Visiting All Points - LeetCode

Can you solve this real interview question? Minimum Time Visiting All Points - On a 2D plane, there are n points with integer coordinates points[i] = [xi, yi]. Return the minimum time in seconds to visit all the points in the order given by points. You can

leetcode.com

 

현 좌표와 전 좌표의 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;
    }
    
};
반응형

+ Recent posts