TapeEquilibrium
function sumArr(array $a){ $n = count($a); $sum = 0; for($i = 0; $i < $n; $i++){ $sum += $a[$i]; } return $sum; } function solution(array $a) { $n = count($a); if($n == 1){ return $a[0]; } $min = null; $p = 1; $sum1 = 0; $sum2 = sumArr($a); do{ $sum1 += $a[$p - 1]; $sum2 -= $a[$p - 1]; $diff = abs($sum1 - $sum2); if($diff == 0){ return 0; }elseif($min === null || $diff < $min){ $min = $diff; } $p++; }while($p < $n); return $min; }
FrogJmp
function solution($x, $y, $d) { $dist = $y - $x; if($dist % $d >= 1){ return ((int)($dist / $d) + 1); }else{ return (int)($dist / $d); } }
PermMissingElem
function solution($a) { $tempArr = []; $missing = 1; $n = count($a); $lookAtTemp = false; for($i = 0; $i < $n; $i++){ if($a[$i] == $missing){ $missing++; $lookAtTemp = true; }else{ $tempArr[$a[$i]] = 1; } while($missing < $n + 1 && $lookAtTemp){ if(isset($tempArr[$missing])){ $missing++; }else{ $lookAtTemp = false; } } } return $missing; }
No comments:
Post a Comment