Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public class Solution {
// X and Y co-ordinates of the points in order.
// Each point is represented by (X.get(i), Y.get(i))
public int coverPoints(ArrayList<Integer> X, ArrayList<Integer> Y) {
if(X.size()<=1 || Y.size()<=1){
return 0;
}
int steps=0;
int startingPoint = 0;
for(int destinationPointIndex = 1 ; destinationPointIndex < X.size() ; destinationPointIndex++){
int destX=X.get(destinationPointIndex);
int destY=Y.get(destinationPointIndex);
int startX=X.get(startingPoint);
int startY=Y.get(startingPoint);
Integer maxDiagonalPath= Math.min(
Math.abs(destX-startX),
Math.abs(destY-startY)
);
int directionX=1;
if(destX<startX){
directionX=-1;
}
int directionY=1;
if(destY<startY){
directionY=-1;
}
int diagomalX=startX+maxDiagonalPath*directionX;
int diagomalY=startY+maxDiagonalPath*directionY;
Integer maxLiniearPath= Math.max(
Math.abs(destX-diagomalX),
Math.abs(destY-diagomalY)
);
startingPoint=destinationPointIndex;
steps+=maxDiagonalPath+maxLiniearPath;
}
return steps;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment