10月底之前有ESTEE LAUDER的deal 吗?# PennySaver - 省钱一族
m*7
1 楼
大家新年好~!
leetcode上 stock III的这个 dp 解法,http://www.cnblogs.com/caijinlong/archive/2013/05/01/3053165.html
上讲的dp算法看懂了,但是代码实现和算法如何对应起来的,没看懂。。
欢迎大牛们指导~~谢!!
code:
int maxProfit(vector &prices) {
int f[3] = {0};
int g[3] = {0};
int n = prices.size() - 1;
for (int i = 0; i < n; ++i) {
int diff = prices[i+1] - prices[i];
int m = min(i+1, 2);
for (int j = m; j >= 1; --j) {
f[j] = max(f[j], g[j-1]) + diff;
g[j] = max(g[j], f[j]);
}
}
return max(g[1], g[2]);
}
leetcode上 stock III的这个 dp 解法,http://www.cnblogs.com/caijinlong/archive/2013/05/01/3053165.html
上讲的dp算法看懂了,但是代码实现和算法如何对应起来的,没看懂。。
欢迎大牛们指导~~谢!!
code:
int maxProfit(vector
int f[3] = {0};
int g[3] = {0};
int n = prices.size() - 1;
for (int i = 0; i < n; ++i) {
int diff = prices[i+1] - prices[i];
int m = min(i+1, 2);
for (int j = m; j >= 1; --j) {
f[j] = max(f[j], g[j-1]) + diff;
g[j] = max(g[j], f[j]);
}
}
return max(g[1], g[2]);
}