My approach to the problem


#1

I’m glad for the simplest solutions people have posted here. But here goes my approach… I tried travelling the diagonal first and then reaching to the destination straight.

int findDis(int x1, int y1, int x2, int y2)
{
int min = abs(x1-x2) < abs(y1-y2) ? abs(x1-x2) : abs(y1-y2);
int f1 = x1>x2 ? 1 : -1;
int f2 = y1>y2 ? 1 : -1;
x1=x1-f1 * min;
y1=y1-f2 * min;

return abs(x1-x2)+abs(y1-y2)+min;

}

int Solution::coverPoints(vector &A, vector &B) {

int a=A.size(), b=B.size();
if(a==1 && b==1)
return 0;

int sum=0;
for(int i=0;i<a-1; i++)
{
    sum+=findDis(A[i], B[i], A[i+1], B[i+1]);
}
return sum;

}