Conditions | 3 |
Paths | 3 |
Total Lines | 12 |
Code Lines | 7 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
29 | public static function maxProfit2(array $prices): int |
||
30 | { |
||
31 | if (count($prices) <= 0) { |
||
32 | return 0; |
||
33 | } |
||
34 | [$maxProfit, $minPrice] = [0, -$prices[0]]; |
||
35 | foreach ($prices as $price) { |
||
36 | $maxProfit = max($maxProfit, $minPrice + $price); |
||
37 | $minPrice = max($minPrice, $maxProfit - $price); |
||
38 | } |
||
39 | |||
40 | return $maxProfit; |
||
41 | } |
||
71 |