-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
45 lines (39 loc) · 1.07 KB
/
main.cpp
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
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
using namespace std;
class Solution {
public:
int maxProfit(vector<int>& prices) {
int k = 1;
// 0.Move index to 1-based
int n = prices.size();
prices.insert(prices.begin(), 0);
// 1.Define f, initialize 负无穷
vector<vector<vector<int>>> f(
n + 1, vector<vector<int>>(2, vector<int>(k + 1, -1e9)));
f[0][0][0] = 0; // 合法起点
// 2. Loop over all states;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < 2; j++) {
for (int p = 0; p <= k; p++) {
// 3. Copy decisions 抄决策方程
if (p > 0)
f[i][1][p] = max(f[i][1][p], f[i - 1][0][p - 1] - prices[i]);
f[i][0][p] = max(f[i][0][p], f[i - 1][1][p] + prices[i]);
f[i][j][p] = max(f[i][j][p], f[i - 1][j][p]);
}
}
}
// 4. Return target
int ans = -1e9;
for (int i = 0; i <= k; i++) {
ans = max(ans, f[n][0][i]);
}
return ans;
}
};
int main() {
Solution s;
vector<int> prices = {7, 1, 5, 3, 6, 4};
cout << s.maxProfit(prices) << endl;
return 0;
}