aboutsummaryrefslogtreecommitdiff
path: root/challenge-151/lubos-kolouch/php/ch-2.php
blob: b31e33d9ad56f0e71f971666ee53728fcff6c9d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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');

}
?>