(919) 964-9842
(919) 964-9842

Hackerrank in PHP: Jumping on Clouds

This one from Hackerrank stumped me for a moment and then I cleared everything I was doing and got it in one try. I think my problem with these is I overthink them too much. Anyways, Emma is jumping on sequentially numbered clouds. She cannot jump on clouds that equal 1 (this threw me off). She needs to get there in the shortest amount of jumps.

Time to help Emma jump some clouds!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Complete the jumpingOnClouds function below.
function jumpingOnClouds($c) {
   
    $count = 0;

    for($i = 1; $i < count($c); $i++) {
        if(isset($c[$i + 1]) && $c[$i + 1] == 0) {
            $i++;
            $count++;
        } else {
            $count++;
        }
    }

    return $count;
}

I know, you're asking: "But where is the check for the clouds that are equal to 1?" - Well, we aren't checking for them exclusively, instead we're doing the opposite and checking for if the next cloud is equal to zero. If it is, huzzah, move forward and increment.

I don't like this one because it still doesn't make complete sense to me, but it passes all the Hackerrank tests. So I will leave it be. Do you have a better solution to this weird one?

Categories: Development

Leave a Reply

Your email address will not be published. Required fields are marked *