just getting into java. What is the best way to encounter this issue? I understand what is happening, and I am asking for the best way to solve it, cause my solution is a bit ugly I might say.
public static int jumpingOnClouds(List<Integer> c) {
int size = c.size();
int jumpx = 0;
for(int i = 0; i<size; i++){
jumpx++;
if(c.get(i+2)==0)i++; //Last 2 iteration will gives the error.
}
return jumpx;
}
My newbie solution
public static int jumpingOnClouds(List<Integer> c) {
int size = c.size();
int jumpx = 0;
for(int i = 0; i<size; i++){
jumpx++;
if (!(i+1>=size || i+2>=size)){ //adding this to avoid c.get(i+2) to be executed.
if(c.get(i+2)==0)i++;
}
if(i+2>=size || i+1>=size)break; //adding this to avoid jumpx++ to be executed.
}
return jumpx;
}
I might be a bit blur, but I hope someone can explain and shows the best one (with the code). Thank you.