Created
April 10, 2024 18:43
-
-
Save Nelex/828535e9cc03b4e6bd5983b25a699a9f to your computer and use it in GitHub Desktop.
Answer
This file contains hidden or 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
| <?php | |
| $levels[] = [1, 0, 2, 1,0,1,3,2,1,2,1]; //6 | |
| $levels[] = [3, 0, 2, 0, 4]; //7 | |
| $levels[] = [2, 0,0, 2]; //4 | |
| function estimate(array $levels): int | |
| { | |
| $count = 0; | |
| $maxHeight = max($levels); | |
| $map = extractMap($levels); | |
| $columns = count($map); | |
| echo PHP_EOL."Max: ".$maxHeight." Columns: ".$columns; | |
| for ($currentRow = 0; $currentRow <= $maxHeight; $currentRow++) { | |
| $leftPoint = 0; | |
| $rightPoint = 0; | |
| $lpSet = false; | |
| for ($currentColumn = 0; $currentColumn < $columns; $currentColumn++) { | |
| if (!$lpSet && !empty($map[$currentColumn][$currentRow])) { | |
| $leftPoint = $currentColumn; | |
| $lpSet = true; | |
| } | |
| if (!empty($map[$currentColumn][$currentRow])) { | |
| $rightPoint = $currentColumn; | |
| } | |
| } | |
| if ($rightPoint > ($leftPoint+1)){ | |
| echo PHP_EOL."For row #".$currentRow." left point: ".$leftPoint." right point: ".$rightPoint; | |
| for ($currentColumn = $leftPoint+1; $currentColumn < $rightPoint; $currentColumn++) { | |
| if (empty($map[$currentColumn][$currentRow])) { | |
| $count++; | |
| } | |
| } | |
| } | |
| } | |
| return $count; | |
| } | |
| function extractMap(array $levels): array | |
| { | |
| $map = []; | |
| $max = max($levels); | |
| $columns = count($levels); | |
| for ($i = 0; $i < $columns; $i++) { | |
| for ($j = 0; $j < $max; $j++) { | |
| $map[$i][$j] = ($levels[$i] <= $j) ? 0 : 1; | |
| } | |
| } | |
| return $map; | |
| } | |
| for ($i = 0; $i < count($levels); $i++) { | |
| echo PHP_EOL."For level #".$i." can be filled is: ".estimate($levels[$i]).PHP_EOL; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment