Here is another one. Didn't use collections this time.
Original task is here: LINK
My version is below
package com.andrjushina.projects.codingchallenges;
public class JumpingOnClouds {
public static void main(String[] args) {
int clouds[] = {0,0,1,0,0,0,1,0,0,0,0,0,1};
int jumps = jumpAhead(clouds);
System.out.println("jumps: " + jumps);
}
static int jumpAhead(int[] c) {
int jumps = 0;
// processing short arrays (less than 3) separately
if (c.length == 2) {
jumps++;
}
// arrays with length 3 and more
for (int i = 2; i < c.length; i++) {
if (i < c.length - 2 && c[i - 2] == 0 && c[i] == 0) {
jumps++;
i++;
} else {
jumps++;
}
}
return jumps;
}
}
No comments:
Post a Comment