FrogRiverOne
function solution($X, $A) { $c = count($A); $current = 0; $leaves = []; for($i = 0; $i < $c; $i++){ if($current + 1 == $A[$i]){ $current++; } while($current < $X){ if(!isset($leaves[$current + 1])){ break; }else{ $current++; } } if($current >= $X){ return $i; } $leaves[$A[$i]] = 1; } return -1; }
PermCheck
function solution($a) { $tempArr = []; $n = count($a); for($i = 0; $i < $n; $i++){ if($a[$i] > $n || isset($tempArr[$a[$i]])){ return 0; } $tempArr[$a[$i]] = 1; } return 1; }
MissingInteger
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; }
MaxCounters
class ct{ private $arr; private $ct; public function __construct($n){ $this->ct = $n; for($i = 0; $i < $n; $i++){ $this->arr[$i] = 0; } } public function increase($x){ $this->arr[$x-1] += 1; } public function maxCounter(){ $max = 0; foreach($this->arr as $val){ $max = ($max < $val ? $val : $max); } for($i = 0; $i <= $this->ct; $i++){ $this->arr[$i] = $max; } } public function getArr(){ return $this->arr; } } function solution($n, $a) { $counter = new ct($n); foreach($a as $val){ if($val >= 1 && $val <= $n){ $counter->increase($val); }elseif($val == $n + 1){ $counter->maxCounter(); } } return $counter->getArr(); }
No comments:
Post a Comment