Best time to buy and sell stock III --- LeetCode
来源:互联网 发布:淘宝达人登录 编辑:程序博客网 时间:2024/06/06 04:12
题目:
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete at most two transactions.
Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
思路:这次限定了次数 那么可以以某一天为分界点 在这一天之前最大的盈利和这一天之后的最大盈利 这两个盈利之和变为两次交易的最大盈利
#include <iostream>#include <vector>using namespace std;/* 思路:可以将两次分为历史和将来 从某一天开始 历史的最好盈利和将来的最好盈利 这两者的和 即为买卖两次的最佳盈利 */int SellStockThird(vector<int>& vec){vector<int> share(vec.size(),0);vector<int> his(vec.size(),0);vector<int>fur(vec.size(),0);int i,j=0;for(i=1;i<vec.size();i++)share[i] = vec[i]-vec[i-1];int cur=share[0];int sum = share[0];for(i=1;i<vec.size();i++){if(cur < 0) cur = share[i];else{cur+=share[i];his[i] = cur;} if(sum < cur) { sum = cur; his[i] = sum; } else his[i] = his[i-1];}cur = share[share.size()-1];sum = cur;for(i=vec.size()-2;i>=0;i--){if(cur < 0) cur = share[i];else{cur+=share[i];fur[i] = cur;} if(sum < cur) { sum = cur; fur[i] = sum; } else fur[i] = fur[i+1];}sum =0;for(i=0;i<his.size()-1;i++)if(sum < his[i]+fur[i+1])sum = his[i]+fur[i+1];return sum;}int main(){int array[]={12,8,10,6,15,18,10};vector<int> vec(array,array+sizeof(array)/sizeof(int));cout<<SellStockThird(vec);return 0;}
现在我们最多可以进行两次交易。我们仍然使用动态规划来完成,事实上可以解决非常通用的情况,也就是最多进行k次交易的情况。
这里我们先解释最多可以进行k次交易的算法,然后最多进行两次我们只需要把k取成2即可。我们还是使用“局部最优和全局最优解法”。我们维护两种量,一个是当前到达第i天可以最多进行j次交易,最好的利润是多少(global[i][j]),另一个是当前到达第i天,最多可进行j次交易,并且最后一次交易在当天卖出的最好的利润是多少(local[i][j])。下面我们来看递推式,全局的比较简单,
global[i][j]=max(local[i][j],global[i-1][j]),
也就是去当前局部最好的,和过往全局最好的中大的那个(因为最后一次交易如果包含当前天一定在局部最好的里面,否则一定在过往全局最优的里面)。对于局部变量的维护,递推式是local[i][j]=max(global[i-1][j-1]+max(diff,0),local[i-1][j]+diff),
也就是看两个量,第一个是全局到i-1天进行j-1次交易,然后加上今天的交易,如果今天是赚钱的话(也就是前面只要j-1次交易,最后一次交易取当前天),第二个量则是取local第i-1天j次交易,然后加上今天的差值(这里因为local[i-1][j]比如包含第i-1天卖出的交易,所以现在变成第i天卖出,并不会增加交易次数,而且这里无论diff是不是大于0都一定要加上,因为否则就不满足local[i][j]必须在最后一天卖出的条件了)。上面的算法中对于天数需要一次扫描,而每次要对交易次数进行递推式求解,所以时间复杂度是O(n*k),如果是最多进行两次交易,那么复杂度还是O(n)。空间上只需要维护当天数据皆可以,所以是O(k),当k=2,则是O(1)。代码如下:
public int maxProfit(int[] prices) { if(prices==null || prices.length==0) return 0; int[] local = new int[3]; int[] global = new int[3]; for(int i=0;i<prices.length-1;i++) { int diff = prices[i+1]-prices[i]; for(int j=2;j>=1;j--) { local[j] = Math.max(global[j-1]+(diff>0?diff:0), local[j]+diff); global[j] = Math.max(local[j],global[j]); } } return global[2]; }
1 0
- 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 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]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
- JAX-WS简单例子
- HDU 5195 线段树
- JavaScript异步编程 回调函数
- Sicily 1389. Linear Pachinko
- 第四周项目一---三角类函数构造4
- Best time to buy and sell stock III --- LeetCode
- C++虚函数及虚函数表解析
- 拓扑排序+线段树(hdu5195)
- LeetCode Best Time to Buy and Sell Stock
- DB2的sql函数
- Android之ViewPager(实现应用程序只启动一次引导界面 )(四)
- java加密技术-双向加密
- Matlab 如何输入矩阵
- Codeforces Round #297 (Div. 2)——A.B.C.D.E