LC 1266. Minimum Time Visiting All Points

Nilanjan Deb · April 5, 2020

This is my CPP solution.

class Solution {
public:
    int minTimeToVisitAllPoints(vector<vector<int>>& points) {
        int n = points.size(), tot = 0;
        vector<int> current(2,0);
        for(int i=0;i<n-1;i++){
            int x_d = points[i+1][0]-points[i][0];
            int y_d = points[i+1][1]-points[i][1];
            if(abs(x_d)<abs(y_d)){
                tot += abs(x_d);
                int current_y = y_d>0 ? points[i][1] + abs(x_d) : points[i][1] - abs(x_d);
                tot += abs(current_y - points[i+1][1]);
            }
            else{
                tot += abs(y_d);
                int current_x = x_d>0 ? points[i][0] + abs(y_d) : points[i][0] - abs(y_d);
                tot += abs(current_x - points[i+1][0]);
            }
        }
        return tot;
    }
};


Dicussion Forum