[LeetCode] Best Time to Buy and Sell Stock III
2013-08-13 22:46
337 查看
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).
本题我用的方法同样比较麻烦,欢迎大家进行交流~
第一步:合并相同的点
第二步:找到全部的极小值和极大值,先找极小值再找极大值
第三步:第二步找到 mx 组极值,将这 mx 组分割成两部分,分别找这两个部分的最大利润,想当于在两个部分内分别计算Best Time to Buy and Sell Stock 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).
本题我用的方法同样比较麻烦,欢迎大家进行交流~
第一步:合并相同的点
第二步:找到全部的极小值和极大值,先找极小值再找极大值
第三步:第二步找到 mx 组极值,将这 mx 组分割成两部分,分别找这两个部分的最大利润,想当于在两个部分内分别计算Best Time to Buy and Sell Stock I的问题
class Solution { public: int maxProfit(vector<int> &prices) { // Start typing your C/C++ solution below // DO NOT write int main() function int size=prices.size(); if(size==0) return 0; int *newPrices=new int[size]; int *maxPrices=new int[size]; int *minPrices=new int[size]; int n=0; int mx=0; int mn=0; for(int s=0;s<size;s++){//合并相同的点 if(s<size-1&&prices[s]==prices[s+1]) continue; newPrices =prices[s]; n++; } if(newPrices[0]<newPrices[1]){// 分析首位置的情况 minPrices[mn]=newPrices[0]; mn++; } for(int i=1;i<n-1;i++){ if(newPrices[i]>newPrices[i-1]&&newPrices[i]>newPrices[i+1]){ maxPrices[mx]=newPrices[i]; mx++; } if(newPrices[i]<newPrices[i-1]&&newPrices[i]<newPrices[i+1]){ minPrices[mn]=newPrices[i]; mn++; } } if(newPrices[n-1]>newPrices[n-2]){//分析末尾的情况 maxPrices[mx]=newPrices[n-1]; mx++; } else{ minPrices[mn]=newPrices[n-1]; mn++; } int sum_min=0; int sum_max=0; if(mx==0) return 0; if(mx==1) return (maxPrices[0]-minPrices[0]); if(mx==2) return (maxPrices[1]+maxPrices[0]-minPrices[1]-minPrices[0]); int max=0; for(int m=1;m<mx;m++) { int temp_max1=maxPrices[0]-minPrices[0]; int minPos1=0; for(int i=0;i<m;i++) { if(minPrices[i]<minPrices[minPos1]) minPos1=i; if(maxPrices[i]-minPrices[minPos1]>temp_max1) temp_max1=maxPrices[i]-minPrices[minPos1]; } int temp_max2=maxPrices[m]-minPrices[m]; int minPos2=m; for(int j=m;j<mx;j++) { if(minPrices[j]<minPrices[minPos2]) minPos2=j; if(maxPrices[j]-minPrices[minPos2]>temp_max2) temp_max2=maxPrices[j]-minPrices[minPos2]; } if(temp_max2+temp_max1>max) max=temp_max2+temp_max1; //cout<<m<<" "<<temp_max1<<" "<<temp_max2<<endl; } delete []newPrices; delete []maxPrices; delete []minPrices; return max; } };
相关文章推荐
- LeetCode Best Time to Buy and Sell Stock I II III
- [LeetCode] Algorithms-45. 123. 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 (股票买卖时机问题3)
- 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] #121#122#123#188 Best Time to Buy and Sell Stock I & II & III & IV
- 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 I && II && III
- leetcode 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 Solution
- [leetcode]Best Time to Buy and Sell Stock III
- leetcode_question_123 Best Time to Buy and Sell Stock III
- Best Time to Buy and Sell Stock III -- LeetCode
- LeetCode题解系列--123. Best Time to Buy and Sell Stock III