Skip to content

Commit 5e71580

Browse files
authored
Merge pull request #123 from sir-gon/feature/jumping-on-the-clouds
Feature/jumping on the clouds
2 parents 3d1ce53 + 205cc5f commit 5e71580

File tree

2 files changed

+168
-0
lines changed

2 files changed

+168
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import logger from '../../logger';
2+
3+
import { jumpingOnClouds } from './jumpingOnClouds';
4+
5+
describe('Jumping on the Clouds', () => {
6+
it('Jumping on the Clouds Test case 0', () => {
7+
expect.assertions(1);
8+
9+
const input = [0, 0, 1, 0, 0, 1, 0];
10+
const solutionFound = 4;
11+
12+
const calculated = jumpingOnClouds(input);
13+
14+
logger.info(`Jumping on the Clouds Test case 0: ${calculated}`);
15+
16+
expect(calculated).toBe(solutionFound);
17+
});
18+
19+
it('Jumping on the Clouds Test case 1', () => {
20+
expect.assertions(1);
21+
22+
const input = [0, 0, 0, 1, 0, 0];
23+
const solutionFound = 3;
24+
25+
const calculated = jumpingOnClouds(input);
26+
27+
logger.info(`Jumping on the Clouds Test case 1: ${calculated}`);
28+
29+
expect(calculated).toBe(solutionFound);
30+
});
31+
});
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/**
2+
* Jumping on the Clouds
3+
*
4+
* https://www.hackerrank.com/challenges/jumping-on-the-clouds
5+
*
6+
* There is a new mobile game that starts with consecutively numbered clouds.
7+
* Some of the clouds are thunderheads and others are cumulus. The player can
8+
* jump on any cumulus cloud having a number that is equal to the number of the
9+
* current cloud plus 1 or 2. The player must avoid the thunderheads. Determine
10+
* the minimum number of jumps it will take to jump from the starting postion
11+
* to the last cloud. It is always possible to win the game.
12+
*
13+
* For each game, you will get an array of clouds numbered 0 if they are safe
14+
* or 1 if they must be avoided.
15+
*
16+
* # Example
17+
* c = [0, 1, 0, 0, 0, 1, 0]
18+
*
19+
* Index the array from 0...6. The number on each cloud is its index in the
20+
* list so the player must avoid the clouds at indices 1 and 5. They could
21+
* follow these two paths: 0 -> 2 -> 4 -> 6 or 0 -> 2 -> 3 -> 4 -> 6.
22+
* The first path takes 3 jumps while the second takes 4. Return 3.
23+
*
24+
* # Function Description
25+
*
26+
* Complete the jumpingOnClouds function in the editor below.
27+
*
28+
* jumpingOnClouds has the following parameter(s):
29+
*
30+
* * int c[n]: an array of binary integers
31+
*
32+
* # Returns
33+
* * int: the minimum number of jumps required
34+
*
35+
* # Input Format
36+
* The first line contains an integer n, the total number of clouds.
37+
* The second line contains n space-separated binary integers describing
38+
* clouds c[i] where 0 <= i < n.
39+
*
40+
* # Constraints
41+
* 2 <= n < 100
42+
* c[i] ∈ {0, 1}
43+
* c[0] = c[m -1] = 0
44+
*
45+
* # Output format
46+
* Print the minimum number of jumps needed to win the game.
47+
*
48+
* # Sample Input 0
49+
* ```
50+
* 7
51+
* 0 0 1 0 0 1 0
52+
*
53+
* # Sample Output 0
54+
* ```
55+
* 4
56+
* ```
57+
*
58+
* # Explanation 0:
59+
*
60+
* The player must avoid c[2] and c[5]. The game can be won with a minimum
61+
* of 4 jumps:
62+
*
63+
* ```mermaid
64+
* flowchart BT
65+
* 0 -.- 1 -.- 2 -.- 3 -.- 4 -.- 5 -.- 6
66+
* 0 --> 1 --> 3 --> 4 --> 6
67+
*
68+
* 0(0):::cumulus
69+
* 1(1):::cumulus
70+
* 2(2):::storm
71+
* 3(3):::cumulus
72+
* 4(4):::cumulus
73+
* 5(5):::storm
74+
* 6(6):::cumulus
75+
*
76+
* linkStyle default stroke:blue
77+
* classDef cumulus fill:#90FE96
78+
* classDef storm fill:#FE5C5E
79+
* ```
80+
*
81+
* # Sample Input 1
82+
* ```
83+
* 6
84+
* 0 0 0 0 1 0
85+
* ```
86+
*
87+
* # Sample Output 1
88+
* ```
89+
* 3
90+
* ```
91+
*
92+
* # Explanation 1:
93+
* The only thundercloud to avoid is c[4]. The game can be won in 3 jumps:
94+
*
95+
* ```mermaid
96+
* flowchart BT
97+
* 0 -.- 1 -.- 2 -.- 3 -.- 4 -.- 5
98+
* 0 --> 2 --> 3 --> 5
99+
*
100+
* 0(0):::cumulus
101+
* 1(1):::cumulus
102+
* 2(2):::cumulus
103+
* 3(3):::cumulus
104+
* 4(4):::storm
105+
* 5(5):::cumulus
106+
*
107+
* linkStyle default stroke:blue
108+
* classDef cumulus fill:#90FE96
109+
* classDef storm fill:#FE5C5E
110+
* ```
111+
*/
112+
113+
import { logger as console } from '../../logger';
114+
115+
export function jumpingOnClouds(c: number[]): number {
116+
let result = 0;
117+
const end = false;
118+
let key = 0;
119+
120+
console.debug(c);
121+
122+
while (key < c.length || end) {
123+
if (key + 2 < c.length && c[key + 2] === 0) {
124+
result += 1;
125+
key += 2;
126+
} else if (key + 1 < c.length && c[key + 1] === 0) {
127+
result += 1;
128+
key += 1;
129+
} else {
130+
key += 1;
131+
}
132+
}
133+
134+
return result;
135+
}
136+
137+
export default { jumpingOnClouds };

0 commit comments

Comments
 (0)