We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
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;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Join us
Create a HackerRank account
Be part of a 26 million-strong community of developers
Please signup or login in order to view this challenge
Jumping on the Clouds
You are viewing a single comment's thread. Return to all 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) {