FrogJmp

Solution

This java solution to codility problem FrogJmp scored 100%

Logic: Find the distance between X and Y, then divide that distance by the hop size that is D. If there is remainder then one more hop is needed so add 1 to the hops. Return the hops. Can you make this better?

public class FrogJump {

    //Detected time complexity : 0(1)
    public int solution(int X, int Y, int D){
        int distance = Y - X;
        int hops = distance/D;

        if (distance%D >0) {
            hops++;
        }
        return hops;
    }

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s