-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path709.php
More file actions
33 lines (28 loc) · 788 Bytes
/
709.php
File metadata and controls
33 lines (28 loc) · 788 Bytes
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
<?php
class Solution {
/**
* @param Integer[] $prices
* @return Integer
*/
function maxProfit($prices)
{
if (count($prices) <= 1) return 0;
$dp[0] = 0;
for ($i = 1; $i < count($prices); $i ++) {
$dp[$i] = $dp[$i - 1];
for ($j = $i - 1; $j >= 0; $j --) {
$profit = $prices[$i] - $prices[$j];
if ($profit < 0) continue;
if ($j >= 2) {
$dp[$i] = max($profit + $dp[$j - 2], $dp[$i]);
} else {
$dp[$i] = max($profit, $dp[$i]);
}
}
}
return $dp[count($prices) - 1];
}
}
$prices = [1,2];
$sol = new Solution();
var_dump($sol->maxProfit($prices));