I'm posting about my solutions to the HackerRank challenges as a way to improve my learning, reinforce my knowledge, and establish an understanding of the concepts covered. If I help someone by sharing along the way, even better!
Jumping On Clouds is one of the initial challenges with 'EASY' difficulty.
Emma is playing a new mobile game that starts with consecutively numbered clouds. Some of the clouds are thunderheads and some are cumulus. She can jump into any cumulus cloud with a number equal to the current number of clouds plus 1 or 2. She must avoid the storm clouds. Determine the minimum number of jumps that it will take Emma to jump from her starting position to the last cloud. It is always possible to win the game.
For each game, Emma will be given an array of clouds numbered 0
if they are safe or 1
if they need to be avoided. For example, c = [0, 1, 0, 0, 0, 1, 0]
indexed from 0...6
. The number in each cloud is its index in the list, so it would have to avoid clouds with indices 1 and 5. It could take the following two paths: 0 -> 2 -> 4 -> 6
or 0 -> 2 -> 3 -> 4 -> 6
. The first path takes 3
hops while the second takes 4
.
A function that returns the minimum number of required jumps, as an integer.
0 0 1 0 0 1 0
4
function jumpingOnClouds (c ) {
let steps = 0 ,
i = 0 ;
while (i < c .length - 1 ) {
if ((i + 2 < c .length ) && c [i + 2] == 0 ) {
i += 2 ;
} else {
i++ ;
}
steps++ ;
}
return steps;
}
let steps = 0 ,
i = 0 ;
First I created two variables; steps
to keep count of the minimum number of steps required; i
to iterate through the whileloop;
I decided on a whileloop instead of a forloop because for the first statement if ((i + 2 < c.length) && c[i + 2] == 0)
, I want to increment i += 2
and not just by 1.
I always try to jump two clouds. If not possible because of storm cloud, jump 1 cloud. So I do this until the pointer is finally on the last cloud.
Finally, I finalize the function by returning the final count.
Other languages that I speak
def jumpingOnClouds (c )
steps = 0
i = 0
while (i < c .length - 1 ) do
if ((i + 2 < c .length ) && c [i + 2] == 0 )
i += 2
else
i += 1
end
steps += 1
end
steps
end
def jumpingOnClouds (c ):
steps = 0
i = 0
while (i < len (c ) - 1 ):
if ((i + 2 < len (c )) and c [i + 2] == 0 ):
i += 2
else :
i += 1
end
steps += 1
return steps
func jumpingOnClouds (c []int32 ) int32 {
var steps int32
i := 0
for {
if i + 2 < len (c ) && c [i + 2] == 0 {
i += 2
} else {
i++
}
steps++
if i <= len (c ) - 1 {
break
}
}
return steps
}
def jumpingOnClouds (c : Array [Int ]): Int = {
var steps = 0
var i = 0
while (i < c .length - 1 ) {
if ((i + 2 < c .length ) && c (i + 2) == 0 ) {
i += 2
} else {
i += 1
}
steps += 1
}
return steps
static int jumpingOnClouds (int [] c ) {
int steps = 0 ,
i = 0 ;
while (i < c .length - 1 ) {
if ((i + 2 < c .length ) && c [i + 2] == 0 ) {
i += 2 ;
} else {
i += 1 ;
}
steps += 1 ;
}
return steps;
function jumpingOnClouds ($c ) {
$steps = 0 ;
$i = 0 ;
while ($i < count ($c ) - 1 ) {
if (($i + 2 < count ($c )) && $c [$i + 2] == 0 ) {
$i += 2 ;
} else {
$i++ ;
}
$steps++ ;
}
return $steps;
Thought: We can judge the heart of a man by his treatment of animals.