Created
November 9, 2020 11:47
-
-
Save KonovalovMaxim/c4a42cc167caa50e53d6b3e3fbf2a6f2 to your computer and use it in GitHub Desktop.
This file contains 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
class Solution { | |
private $arr = []; | |
/** | |
* @param Integer[] $arr | |
* @return String | |
*/ | |
function largestTimeFromDigits($arr) { | |
$this->arr = $arr; | |
$two = $this->getOneOf([2]); | |
if ($two !== false) { | |
$b = $this->getOneOf([3,2,1,0]); | |
if($b !== false) { | |
$c = $this->getOneOf([5,4,3,2,1,0]); | |
if($c !== false) { | |
$d = $this->getBest(); | |
return "$two$b:$c$d"; | |
} else { | |
$this->putBack($b); | |
$this->putBack($two); | |
} | |
} else { | |
$this->putBack($two); | |
} | |
} | |
$one = $this->getOneOf([1]); | |
if($one !== false) { | |
$b = $this->getBest(); | |
$c = $this->getOneOf([5,4,3,2,1,0]); | |
if($c !== false) { | |
$d = $this->getBest(); | |
return "$one$b:$c$d"; | |
} else { | |
$this->putBack($b); | |
$this->putBack($one); | |
} | |
} | |
$zero = $this->getOneOf([0]); | |
if($zero !== false) { | |
$b = $this->getBest(); | |
$c = $this->getOneOf([5,4,3,2,1,0]); | |
if($c !== false) { | |
$d = $this->getBest(); | |
return "$zero$b:$c$d"; | |
} else { | |
$this->putBack($zero); | |
$this->putBack($b); | |
} | |
} | |
return ""; | |
} | |
function putBack($num) { | |
$this->arr[] = $num; | |
$this->arr = array_values($this->arr); | |
} | |
function getNum ($num) { | |
for($i = 0;$i<count($this->arr);$i++) { | |
if($this->arr[$i] == $num) { | |
unset($this->arr[$i]); | |
$this->arr = array_values($this->arr); | |
return $num; | |
} | |
} | |
return false; | |
} | |
function getOneOf($a) { | |
foreach($a as $i=>$v) { | |
$val = $this->getNum($v); | |
if($val !== false) { | |
// unset($this->arr[$i]); | |
$this->arr = array_values($this->arr); | |
return $val; | |
} | |
} | |
return false; | |
} | |
function getBest() | |
{ | |
$max = PHP_INT_MIN; | |
$maxIndex = false; | |
// var_dump($this->arr); | |
foreach($this->arr as $i => $v) { | |
if($v > $max) { | |
$max = $v; | |
$maxIndex = $i; | |
} | |
} | |
unset($this->arr[$maxIndex]); | |
$this->arr = array_values($this->arr); | |
return $max; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment