Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielgoncharov/0646c510f8e68fa1f2ce13477dc5f386 to your computer and use it in GitHub Desktop.
Save danielgoncharov/0646c510f8e68fa1f2ce13477dc5f386 to your computer and use it in GitHub Desktop.
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