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.
Jumping on the Clouds
Jumping on the Clouds
+ 0 comments Java Simpler Solution
public static int jumpingOnClouds(List<Integer> c) { int jumps = 0; jumps += c.size() == 2 ? 1 : 0; for(int i = 2; i < c.size(); i+=2){ if(c.get(i) == 1){ i -= 1; } jumps++; i -= i + 2 >= c.size() ? 1 : 0; } return jumps; }
JavaScript Simpler Solution
function jumpingOnClouds(c) { var jumps = 0 jumps += 2 == c.length ? 1 : 0 for(var i = 2; i < c.length; i+=2){ if(c[i] == 1){ i -= 1 } jumps++ i -= i + 2 >= c.length ? 1 : 0 } return jumps }
+ 0 comments Simple solution in java Hope you like it : )
public static int jumpingOnClouds(List<Integer> c) { int jumps = 0; int n =c.size()-1; int lastPos=0; for(int i = 2;i<=n;i+=2){ if(c.get(i)==1){ i--; jumps++; } else{ jumps++; } lastPos = i; } if(lastPos==n-1){ jumps++; } return jumps; }
+ 0 comments This is my Java 8 solution. Feel free to ask me any questions.
public static int jumpingOnClouds(List<Integer> c) { //Start jumping at index 0 int position = 0; int jumps = 1; int n = c.size(); while(position < n - 1) { //Examine the next two step //Jump out the clouds if(position + 2 >= n - 1) { break; //Move to the next to step if it's good } else if(c.get(position + 2) == 0) { position = position + 2; //Otherwise } else position++; //update jumps jumps++; } return jumps; }
+ 0 comments C#
public static int jumpingOnClouds(List<int> c) { var count = 0; var i = 0; var n = c.Count; while (i < n - 1) { if (i + 2 < n && c[i + 2] == 0) { i += 2; } else { i += 1; } count++; } return count; }
+ 0 comments With Python 3:
Solution 1:
def jumpingOnClouds(c): val = 0 steps = 0 while val < len(c) - 1: if val + 3 <= len(c) and (c[val + 1] == 1 or c[val + 2] == 0 or c[val + 3] == 1): val += 2 else: val += 1 steps += 1 return steps
Solution 2:
def jumpingOnClouds(c): val = 0 steps = 0 while val < len(c) - 1: val = val + 2 if (val + 3 <= len(c) and (c[val + 1] == 1 or c[val + 2] == 0 or c[val + 3] == 1)) else val + 1 steps += 1 return steps
Load more conversations
Sort 3129 Discussions, By:
Please Login in order to post a comment