Conditions | 4 |
Paths | 4 |
Total Lines | 18 |
Code Lines | 12 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
9 | public static function maxProfit(array $prices): int |
||
10 | { |
||
11 | $n = count($prices); |
||
12 | if ($n <= 0) { |
||
13 | return 0; |
||
14 | } |
||
15 | $dp = array_fill(0, $n, array_fill(0, $n, [0, 0])); |
||
16 | foreach ($prices as $i => $price) { |
||
17 | if ($i - 1 < 0) { |
||
18 | $dp[0][0] = 0; |
||
19 | $dp[0][1] = -$price; |
||
20 | } else { |
||
21 | $dp[$i][0] = max($dp[$i - 1][0], $dp[$i - 1][1] + $price); |
||
22 | $dp[$i][1] = max($dp[$i - 1][1], $dp[$i - 1][0] - $price); |
||
23 | } |
||
24 | } |
||
25 | |||
26 | return $dp[$n - 1][0]; |
||
27 | } |
||
71 |