Created
January 9, 2015 02:46
-
-
Save ukisoft/cf778bbbad2125c35801 to your computer and use it in GitHub Desktop.
Fence Repair ref: http://qiita.com/ukisoft/items/1600072d32ca199b0694
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 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