Skip to content

Instantly share code, notes, and snippets.

@ukisoft
Created January 9, 2015 02:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ukisoft/cf778bbbad2125c35801 to your computer and use it in GitHub Desktop.
Save ukisoft/cf778bbbad2125c35801 to your computer and use it in GitHub Desktop.
class FenceRepair
{
public static function calc($boardsLength)
{
$cost = 0;
asort($boardsLength);
while (count($boardsLength) > 1) {
$firstBoard = array_shift($boardsLength);
$secondBoard = array_shift($boardsLength);
$cost += $newBoard = $firstBoard + $secondBoard;
$boardsLength = FenceRepair::insertNumToArrayThroughAsort($newBoard, $boardsLength);
}
return $cost;
}
private static function insertNumToArrayThroughAsort($num, $array)
{
foreach ($array as $key => $value) {
if ($value >= $num) {
array_splice($array, $key, 0, $num);
return $array;
}
}
array_push($array, $num);
return $array;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment