Skip to content

Instantly share code, notes, and snippets.

@barakpinchovski
Created September 10, 2019 06:12
Show Gist options
  • Save barakpinchovski/e3665d3b37d24a29d9896cc816da0f85 to your computer and use it in GitHub Desktop.
Save barakpinchovski/e3665d3b37d24a29d9896cc816da0f85 to your computer and use it in GitHub Desktop.
<?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