[leetcode] Best Time to Buy and Sell Stock III
2014-11-11 22:23
197 查看
Best
Time to Buy and Sell Stock III
Time to Buy and Sell Stock III
class Solution { public: int maxProfit(vector<int> &prices) { const int n=prices.size(); if(n<2){ return 0; } vector<int> f(n,0); vector<int> g(n,0); int valley=prices[0]; int peak=prices[n-1]; for(int i=1;i<n;i++){//波谷 valley=min(valley,prices[i]); f[i]=max(f[i-1],prices[i]-valley); } for(int j=n-2;j>=0;j--){//波峰 peak=max(peak,prices[j]); g[j]=max(g[j+1],peak-prices[j]); } int max_profit=0; for(int k=0;k<n;k++){ max_profit=max(max_profit,f[k]+g[k]);//dp } return max_profit; } };
相关文章推荐
- LeetCode: Best Time to Buy and Sell Stock III
- Leetcode: Best Time to Buy and Sell Stock I II III
- [LeetCode 123] - 买入与卖出股票的最佳时机III(Best Time to Buy and Sell Stock III)
- leetcode Best Time to Buy and Sell Stock I&&II&&III
- Leetcode::Best Time to Buy and Sell Stock III
- [leetcode]Best Time to Buy and Sell Stock III
- leetcode 62: Best Time to Buy and Sell Stock III
- [leetcode]Best Time to Buy and Sell Stock III
- [leetcode]Best Time to Buy and Sell Stock III
- [leetcode]Best Time to Buy and Sell Stock III
- LeetCode Best Time to Buy and Sell Stock III
- [Leetcode] Best Time to Buy and Sell Stock III
- [LeetCode] Best Time to Buy and Sell Stock III
- leetcode 16: Best Time to Buy and Sell Stock III
- leetcode - Best Time to Buy and Sell Stock III
- LeetCode - Best Time to Buy and Sell Stock III
- 【leetcode】Best Time to Buy and Sell Stock III
- [LeetCode]Best Time to Buy and Sell Stock III
- LeetCode —— Best Time to Buy and Sell Stock III
- LeetCode_Best Time to Buy and Sell Stock III