Skip to content

Commit 52ee82e

Browse files
committed
added jumping-on-the-clouds problem solution
1 parent 42ff878 commit 52ee82e

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

Jumping-on-the-clouds/main.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env node
2+
3+
4+
// Problem Description at -> https://www.hackerrank.com/challenges/jumping-on-the-clouds/problem
5+
6+
function jumpingOnClouds(c) {
7+
let minPath = 0;
8+
9+
let i = 0;
10+
while (i < c.length - 1) {
11+
if (c[i] == 1) continue;
12+
13+
if (c[i + 2] == 0) {
14+
minPath++;
15+
i += 2;
16+
continue;
17+
}
18+
19+
if (c[i + 1] == 0) {
20+
i += 1;
21+
minPath++;
22+
}
23+
}
24+
25+
return minPath;
26+
}
27+
28+
// Test cases
29+
console.log(jumpingOnClouds([0, 0, 1, 0, 0, 1, 0])); // 4 jumps
30+
console.log(jumpingOnClouds([0, 0, 0, 0, 1, 0])); // 3 jumps
31+
console.log(jumpingOnClouds([0, 1, 0, 0, 1, 0])); // 3 jumps

0 commit comments

Comments
 (0)