123. Best Time to Buy and Sell Stock III
来源:互联网 发布:地面站软件 编辑:程序博客网 时间:2024/06/11 19:46
/*f[k][j]表示到j天为止进行了k次交易,产生的最大利润f[k][j]=max{f[k][j-1],price[j]-price[i]+f[k-1][i]} 0=<i<=j-1 =max{f[k][j-1],price[j]+max{f[k-1][i]-price[i]}} */class Solution {public: int maxProfit(vector<int>& prices) { if(prices.size()<=1)//天数<=1 无法进行交易 return 0; int maxk=2; int tempmax; vector<vector<int>> f(maxk+1,vector<int>(prices.size()+1,0)); for(int k=1;k<=maxk;k++) { tempmax=f[k-1][0]-prices[0]; for(int j=1;j<prices.size();j++) { f[k][j]=max(f[k][j-1],prices[j]+tempmax); tempmax=max(tempmax,f[k-1][j]-prices[j]); } } return f[2][prices.size()-1]; }};
1 0
- LeetCode 123. Best Time to Buy and Sell Stock III
- 123.Best Time to Buy and Sell Stock III
- [Leetcode]123.Best Time to Buy and Sell Stock III
- [Leetcode] 123. Best Time to Buy and Sell Stock III
- 123. Best Time to Buy and Sell Stock III
- [leetcode] 123.Best Time to Buy and Sell Stock III
- 【leetcode】123. Best Time to Buy and Sell Stock III
- 123. Best Time to Buy and Sell Stock III
- [leetcode] 123. Best Time to Buy and Sell Stock III
- 123. Best Time to Buy and Sell Stock III
- 123. Best Time to Buy and Sell Stock III
- leetcode 123. Best Time to Buy and Sell Stock III
- leetcode 123. Best Time to Buy and Sell Stock III
- 123. Best Time to Buy and Sell Stock III
- 123. Best Time to Buy and Sell Stock III
- [LeetCode] 123. Best Time to Buy and Sell Stock III
- 123. Best Time to Buy and Sell Stock III
- Leetcode 123. Best Time to Buy and Sell Stock III
- MYSQL数据库数据拆分之分库分表总结
- C++ string::npos,结合find,或者sscanf
- Oculus Unity Development Guide开发指南(转)
- error C2338: THE_BRACKET_OPERATOR_IS_ONLY_FOR_VECTORS__USE_THE_PARENTHESIS_OPERATOR_INSTEAD
- CodeForces 544D Destroying Roads(最短路)
- 123. Best Time to Buy and Sell Stock III
- UE4初探及问题总结
- Android之图片选择与裁剪
- 【EHCache】EHCache Documentation
- 优先队列及java实现和用法
- Android EditText 使用
- ios上 更改 状态栏(UIStatusBar)的颜色,你值得一看、收藏
- PHP之猴子吃桃
- 七桥问题