I want the same result with less execution time. Is there any better way to write the code so that I can get the same result with minimum execution time.
<?php
$input1 = 5;
$input2 = 1;
$input3 = [9, 5, 10];
$numberOfWalls = count($input3);
$numberOfJumps = 0;
for($i=0; $i<$numberOfWalls; $i++){
if($input1 >= $input3[$i]){
$numberOfJumps += 1;
}else {
$tot = 0;
while(1){
if($tot + $input1 < $input3[$i]){
$tot = $tot + ($input1 - $input2);
$numberOfJumps += 1;
}else{
$tot = $tot + $input1;
$numberOfJumps += 1;
break;
}
}
}
}
echo $numberOfJumps;
How about this:
// input
$forwards = 5;
$backwards = 1;
$walls = [9, 5, 10];
// computation
$oneJump = $forwards-$backwards;
$jumps = 0;
foreach ($walls as $wall) $jumps += ceil(($wall-$backwards)/$oneJump);
// output
echo "Jumps needed = $jumps";