• + 0 comments

    Key factor - as per the task, the player always reaches the end. so he either jump 2 positions, or jumps 1 position if 2 lands on a thunder cloud. We try to jump 2 positions, if its a thunder cloud - we return 1 back to a guaranteed safe space. Looping until less than c.size() - 1, as the c.size() -1 is considered finish and player doesnt jump after landing there.

    // public static int jumpingOnClouds(List c) {

        int result = 1;
    
        for (int cloud = 2; cloud < c.size() - 1; cloud += 2) {
            if(c.get(cloud) == 1) cloud--;
            result++;
        }
    
        return result;
    }