Codility FrogJmp strange Java score
Solution in Java 100/100 and O(1) time complexity.
public int solution(int X, int Y, int D) {
return Double.valueOf(Math.ceil((Y - X) / (double) D)).intValue();
}
Both solutions have O(1) time complexity. The problem is that the first solution is returning wrong answers. The performance tests test the answer as well as the time. Your solution failed probably because of precision issues with the use of floats.
For x = 1, y = 1000000000, d = 1, your first solution gives 1000000000 as an answer, and the second gives
999999999. Changing from (float)
to (double)
corrects this result.
In these algorithm tests, it's usually a good idea to avoid floating point arithmetic as much as possible to make it easier to get the exact answers for all cases.
100/100 solution in C# I just
using System;
class Solution {
public int solution(int X, int Y, int D) {
return ((Y - X) + D - 1)/D;
}
}