diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2022-02-12 21:16:34 +0100 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2022-02-12 21:16:34 +0100 |
| commit | a107a05db3ee5843c71f0741b4cbb249809285f2 (patch) | |
| tree | 21ba605f1d22529ca332c68a2b35970afc64db43 | |
| parent | 47782debf427a239672b64a81cddbe0e3eb9935a (diff) | |
| download | perlweeklychallenge-club-a107a05db3ee5843c71f0741b4cbb249809285f2.tar.gz perlweeklychallenge-club-a107a05db3ee5843c71f0741b4cbb249809285f2.tar.bz2 perlweeklychallenge-club-a107a05db3ee5843c71f0741b4cbb249809285f2.zip | |
Challenge 151 Task 2 LK PHP
| -rw-r--r-- | challenge-151/lubos-kolouch/php/ch-2.php | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/challenge-151/lubos-kolouch/php/ch-2.php b/challenge-151/lubos-kolouch/php/ch-2.php new file mode 100644 index 0000000000..b31e33d9ad --- /dev/null +++ b/challenge-151/lubos-kolouch/php/ch-2.php @@ -0,0 +1,41 @@ +<?php + +$cache = []; + +function get_houses_max($houses) { + + global $cache; + + if (in_array(implode(",",$houses), $cache)) { + return $cache[implode(",",$houses)]; + } + + $max_value = 0; + $house_index = 0; + + foreach (array_slice($houses, 2) as $house) { + $next_houses_values = get_houses_max(array_slice($houses, 2+$house_index)); + if ($next_houses_values > $max_value) { + $max_value = $next_houses_values; + } + + $house_index++; + } + + $cache[implode(",",$houses)] = $houses[0] + $max_value; + return $houses[0] + $max_value; + +} + + +if (get_houses_max([2,4,5]) != 7) { + throw new Exception('Failed test 1'); +} + +$cache = []; + +if (get_houses_max([4, 2, 3, 6, 5, 3]) != 13) { + throw new Exception('Failed test 2'); + +} +?> |
