Cpp O(n) solution


#1

int Solution::coverPoints(vector &A, vector &B) {
int n=A.size();
int ans=0;
for(int i=1;i<n;i++)
{
int x=abs(A[i]-A[i-1]);
int y=abs(B[i]-B[i-1]);
int t=min(x,y);
ans+=t;
x-=t;
y-=t;
ans+=x+y;
}
return ans;
}