Conditions | 4 |
Paths | 4 |
Total Lines | 14 |
Code Lines | 7 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
27 | public static function minimumTotal2(array $triangle) |
||
28 | { |
||
29 | if (0 === $n = count($triangle)) { |
||
30 | return $n; |
||
31 | } |
||
32 | |||
33 | $ans = $triangle[$n - 1]; |
||
34 | for ($i = $n - 2; $i >= 0; $i--) { |
||
35 | for ($j = 0; $j <= $i; $j++) { |
||
36 | $ans[$j] = $triangle[$i][$j] + min($ans[$j], $ans[$j + 1]); |
||
37 | } |
||
38 | } |
||
39 | |||
40 | return $ans[0]; |
||
41 | } |
||
43 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: