The frog wants to get on the other side of the road and she needs (y – x)/d jumps to get there. Simple as that. Since that can be decimal number we need to check if it needs to be rounded on lower or greater number. And that’s it. 🙂

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
// Result: 100/100 public static int solution_3_FrogJmp(int x, int y, int d) { double numOfJumpsF = (double)(y - x)/d; int roundNumOfJumps = (int) Math.round(numOfJumpsF); double restOfNumOfJumps = numOfJumpsF; if (roundNumOfJumps > 1) restOfNumOfJumps = numOfJumpsF % roundNumOfJumps; if (restOfNumOfJumps > 0 && restOfNumOfJumps < 0.5) roundNumOfJumps++; return roundNumOfJumps; } |