Created
October 2, 2017 09:02
-
-
Save danielgoncharov/0646c510f8e68fa1f2ce13477dc5f386 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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