Created
September 10, 2019 06:12
-
-
Save barakpinchovski/e3665d3b37d24a29d9896cc816da0f85 to your computer and use it in GitHub Desktop.
PHP solution for https://www.hackerrank.com/challenges/jumping-on-the-clouds/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$exampleClouds = [0,0,1,0,0,1,0]; | |
echo jumpingOnClouds($exampleClouds); | |
// Assumptions for jumpingOnClouds function: | |
// 1. Length: 2 <= count($c) <= 100 | |
// 2. Items: $c[$i] === 0 || $c[$i] === 1 | |
// 3. End-values: $c[0] === $c[ count($c) - 1 ] === 0 | |
function jumpingOnClouds($c) { | |
$min = 0; | |
if (gettype($c) !== 'array' || !count($c)) { | |
return $min; | |
} | |
$len = count($c); | |
define("CUMULUS", 0); | |
// Always advance twice but return once if value is invalid or forbidden | |
for ($i = 2; $i <= $len; $i += 2) { | |
if (!array_key_exists($i, $c) || $c[$i] !== CUMULUS) { | |
$i--; | |
} | |
$min++; | |
} | |
return $min; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment