Conditions | 5 |
Paths | 5 |
Total Lines | 20 |
Code Lines | 13 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
27 | public static function rob2(array $nums): int |
||
28 | { |
||
29 | $n = count($nums); |
||
30 | if ($n === 0) { |
||
31 | return 0; |
||
32 | } |
||
33 | if ($n === 1) { |
||
34 | return $nums[0]; |
||
35 | } |
||
36 | if ($n === 2) { |
||
37 | return max($nums[0], $nums[1]); |
||
38 | } |
||
39 | $prev = $next = array_fill(0, $n + 1, 0); |
||
40 | [$prev[0], $prev[1], $next[0], $next[1]] = [0, $nums[0], 0, 0]; |
||
41 | for ($i = 2; $i <= $n; $i++) { |
||
42 | $prev[$i] = max($prev[$i - 1], $prev[$i - 2] + $nums[$i - 1]); |
||
43 | $next[$i] = max($next[$i - 1], $next[$i - 2] + $nums[$i - 1]); |
||
44 | } |
||
45 | |||
46 | return max($prev[$n - 1], $next[$n]); |
||
47 | } |
||
63 |