Created
April 20, 2017 12:28
-
-
Save hrubantjakub1/294c132752a569613b789ef18794e01b to your computer and use it in GitHub Desktop.
codility solution for frog jump in java
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
// Task is to find out how many jumps is needed to get from distance X to Y. Length of every jump is D. | |
it is one of codility tasks | |
class Solution { | |
// X = beginning, Y = finish, D = distance | |
public int solution(int X, int Y, int D) { | |
int state = X; | |
int count = 0; | |
while (state < Y) { | |
state = state + D; | |
count++; | |
} | |
return count; | |
} | |
public static void main(String[] args) { | |
int X = 10; | |
int Y = 90; | |
int D = 30; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Or you can do it without any loop, e.x.:
public int solution(int X, int Y, int D) {
return (Y-X) % D == 0? (Y-X) / D : (Y-X) /D +1;
}
if the distance (Y-X) is divisible by the jump (D) the frog can make it in distance/jump moves otherwise it has to make one more jump.