C++ O(n) Basic Solnution


#1

int Solution::coverPoints(vector &A, vector &B) {
int ans=0;
int prex=A[0],prey=B[0];
for(int i=1;i<A.size();i++){
int xdiff,ydiff;
if(prex<=A[i]){
xdiff=A[i]-prex;
}else{
xdiff=prex-A[i];
}
if(prey<=B[i]){
ydiff=B[i]-prey;
}else{
ydiff=prey-B[i];
}
ans+= max(xdiff,ydiff);
prex=A[i];
prey=B[i];
}
return ans;
}