This repository has been archived by the owner on May 16, 2021. It is now read-only.
forked from zhuli19901106/leetcode-zhuli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbest-time-to-buy-and-sell-stock-iv_1_AC.cpp
75 lines (69 loc) · 1.92 KB
/
best-time-to-buy-and-sell-stock-iv_1_AC.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// DP without space optimization
// Make sure you understand the relationship between global and local.
#include <algorithm>
#include <vector>
using std::max;
using std::min;
using std::vector;
class Solution {
public:
int maxProfit(int k, vector<int>& prices) {
if (k == 0) {
return 0;
}
auto &a = prices;
int n = a.size();
int cnt = 0;
int i;
for (i = 0; i + 1 < n; ++i) {
if (a[i] < a[i + 1]) {
++cnt;
}
}
if (cnt <= k) {
// Greed is good
return maxProfitAny(a);
}
// Local optimum
vector<vector<int>> dl(k, vector<int>(n, 0));
// Global optimum
vector<vector<int>> dg(k, vector<int>(n, 0));
int diff;
int min_val = a[0];
for (i = 1; i < n; ++i) {
diff = max(0, a[i] - min_val);
dl[0][i] = diff;
dg[0][i] = max(dg[0][i - 1], dl[0][i]);
min_val = min(min_val, a[i]);
}
int j, ki;
for (ki = 1; ki < k; ++ki) {
for (i = ki + 1; i < n; ++i) {
diff = max(0, a[i] - a[ki]);
dg[ki][i] = dl[ki][i] = dg[ki - 1][ki] + diff;
for (j = ki + 1; j < i; ++j) {
diff = max(0, a[i] - a[j]);
dl[ki][i] = max(dl[ki][i], dg[ki - 1][j] + diff);
}
dg[ki][i] = max(dg[ki][i - 1], dl[ki][i]);
}
}
int res = dg[k - 1][n - 1];
dg.clear();
dl.clear();
return res;
}
private:
int maxProfitAny(vector<int>& prices) {
auto &a = prices;
int n = a.size();
int res = 0;
int i;
for (i = 0; i + 1 < n; ++i) {
if (a[i] < a[i + 1]) {
res += a[i + 1] - a[i];
}
}
return res;
}
};