LeetCode: Best Time to Buy and Sell Stock I && II && III
2015-05-18 11:13
597 查看
Title:
Sayyouhaveanarrayforwhichtheithelementisthepriceofagivenstockondayi.
Ifyouwereonlypermittedtocompleteatmostonetransaction(ie,buyoneandselloneshareofthestock),designanalgorithmtofindthemaximumprofit.
思路:对于ith个,找出之前的最小值,然后用当前的减去最小值,看是否是最大的利润。实际上就是动态规划
dp[i]=max{dp[i-1],prices[i]-minprices},minprices是区间[0,1,2...,i-1]内的最低价格
按照股票差价构成新数组prices[1]-prices[0],prices[2]-prices[1],prices[3]-prices[2],...,prices[n-1]-prices[n-2]。求新数组的最大子段和就是我们求得最大利润,假设最大子段和是从新数组第i到第j项,那么子段和=prices[j]-prices[j-1]+prices[j-1]-prices[j-2]+...+prices[i]-prices[i-1]=prices[j]-prices[i-1],即prices[j]是最大价格,prices[i-1]是最小价格,且他们满足前后顺序关系。
Title:
Sayyouhaveanarrayforwhichtheithelementisthepriceofagivenstockondayi.
Designanalgorithmtofindthemaximumprofit.Youmaycompleteasmanytransactionsasyoulike(ie,buyoneandselloneshareofthestockmultipletimes).However,youmaynotengageinmultipletransactionsatthesametime(ie,youmustsellthestockbeforeyoubuyagain).
思路(1):我开始的思路就是找出每个递增子序列
Title:
Sayyouhaveanarrayforwhichtheithelementisthepriceofagivenstockondayi.
Designanalgorithmtofindthemaximumprofit.Youmaycompleteatmosttwotransactions.
思路:
分析:这一题约束最多只能买卖两次股票,并且手上最多也只能持有一支股票。因为不能连续买入两次股票,所以买卖两次肯定分布在前后两个不同的区间。设p(i)=区间[0,1,2...i]的最大利润+区间[i,i+1,....n-1]的最大利润(式子中两个区间内分别只能有一次买卖,这就是第一道题的问题),那么本题的最大利润=max{p[0],p[1],p[2],...,p[n-1]}。根据第一题的算法2,我们可以求区间[0,1,2...i]的最大利润;同理可以从后往前扫描数组求区间[i,i+1,....n-1]的最大利润,其递归式如下:
dp[i-1]=max{dp[i],maxprices-prices[i-1]},maxprices是区间[i,i+1,...,n-1]内的最高价格。
Sayyouhaveanarrayforwhichtheithelementisthepriceofagivenstockondayi.
Ifyouwereonlypermittedtocompleteatmostonetransaction(ie,buyoneandselloneshareofthestock),designanalgorithmtofindthemaximumprofit.
思路:对于ith个,找出之前的最小值,然后用当前的减去最小值,看是否是最大的利润。实际上就是动态规划
dp[i]=max{dp[i-1],prices[i]-minprices},minprices是区间[0,1,2...,i-1]内的最低价格
classSolution{ public: intmaxProfit(vector<int>&prices){ if(prices.size()<2) return0; intmin_price=prices[0]; intprofit=0; for(inti=1;i<prices.size();i++){ min_price=min(min_price,prices[i-1]); profit=max(profit,prices[i]-min_price); } returnprofit; } };
按照股票差价构成新数组prices[1]-prices[0],prices[2]-prices[1],prices[3]-prices[2],...,prices[n-1]-prices[n-2]。求新数组的最大子段和就是我们求得最大利润,假设最大子段和是从新数组第i到第j项,那么子段和=prices[j]-prices[j-1]+prices[j-1]-prices[j-2]+...+prices[i]-prices[i-1]=prices[j]-prices[i-1],即prices[j]是最大价格,prices[i-1]是最小价格,且他们满足前后顺序关系。
classSolution{ public: intmaxProfit(vector<int>&prices){ //IMPORTANT:Pleaseresetanymemberdatayoudeclared,as //thesameSolutioninstancewillbereusedforeachtestcase. intlen=prices.size(); if(len<=1)return0; intres=0,currsum=0; for(inti=1;i<len;i++) { if(currsum<=0) currsum=prices[i]-prices[i-1]; else currsum+=prices[i]-prices[i-1]; if(currsum>res) res=currsum; } returnres; } };
Title:
Sayyouhaveanarrayforwhichtheithelementisthepriceofagivenstockondayi.
Designanalgorithmtofindthemaximumprofit.Youmaycompleteasmanytransactionsasyoulike(ie,buyoneandselloneshareofthestockmultipletimes).However,youmaynotengageinmultipletransactionsatthesametime(ie,youmustsellthestockbeforeyoubuyagain).
思路(1):我开始的思路就是找出每个递增子序列
classSolution{ public: intmaxProfit(vector<int>&prices){ if(prices.size()<2) return0; intfirst=prices[0]; for(inti=1;i<prices.size();i++){ if(prices[i]>first){ while(++i<prices.size()){ if(prices[i]<prices[i-1]) break; } vector<int>v(prices.begin()+i,prices.end()); returnprices[i-1]-first+maxProfit(v); }else{ first=prices[i]; } } return0; } }; 思路(2):还是将原始序列变换成间隔,然后只要将为正的间隔加和就可以了
classSolution{ public: intmaxProfit(vector<int>&prices){ if(prices.size()<2) return0; intprofit=0; for(inti=1;i<prices.size();i++){ intgap=prices[i]-prices[i-1]; if(gap>0) profit+=gap; } returnprofit; } };
Title:
Sayyouhaveanarrayforwhichtheithelementisthepriceofagivenstockondayi.
Designanalgorithmtofindthemaximumprofit.Youmaycompleteatmosttwotransactions.
思路:
分析:这一题约束最多只能买卖两次股票,并且手上最多也只能持有一支股票。因为不能连续买入两次股票,所以买卖两次肯定分布在前后两个不同的区间。设p(i)=区间[0,1,2...i]的最大利润+区间[i,i+1,....n-1]的最大利润(式子中两个区间内分别只能有一次买卖,这就是第一道题的问题),那么本题的最大利润=max{p[0],p[1],p[2],...,p[n-1]}。根据第一题的算法2,我们可以求区间[0,1,2...i]的最大利润;同理可以从后往前扫描数组求区间[i,i+1,....n-1]的最大利润,其递归式如下:
dp[i-1]=max{dp[i],maxprices-prices[i-1]},maxprices是区间[i,i+1,...,n-1]内的最高价格。
classSolution{ public: intmaxProfit(vector<int>&prices){ intsize=prices.size(); if(size<2) return0; vector<int>left(size,0); intmin_price=INT_MAX; intcur_right=0; for(inti=1;i<size;i++){ min_price=min(min_price,prices[i-1]); left[i]=max(left[i-1],prices[i]-min_price); } intresult=left[size-1]; intmax_price=INT_MIN; for(inti=size-2;i>=0;i--){ max_price=max(max_price,prices[i+1]); cur_right=max(cur_right,max_price-prices[i]); result=max(result,left[i]+cur_right); } returnresult; } };
相关文章推荐
- leetcode Best Time to Buy and Sell Stock I&&II&&III
- LeetCode - Best Time to Buy and Sell Stock I && II && III && IV
- [Leetcode] #121#122#123#188 Best Time to Buy and Sell Stock I & II & III & IV
- leetcode day6 -- String to Integer (atoi) && Best Time to Buy and Sell Stock I II III
- leetcode之Best Time to Buy and Sell Stock && II && III
- 【leetcode】Best Time to Buy and Sell Stock I&II&III
- 【leetcode】Best Time to Buy and Sell Stock I && II && III
- leetcode day6 -- String to Integer (atoi) && Best Time to Buy and Sell Stock I II III
- 【LeetCode】Best Time to Buy and Sell Stock I && II && III
- LeetCode:Best Time to Buy and Sell Stock I & II & III
- LeetCode之“动态规划”:Best Time to Buy and Sell Stock I && II && III && IV
- [Leetcode][JAVA] Best Time to Buy and Sell Stock I, II, III
- LeetCode Best Time to Buy and Sell Stock I,II,III
- best-time-to-buy-and-sell-stock I &&II && III && IVbest-time-to-buy-and-sell-stock-ii
- [leetcode]_Best Time to Buy and Sell Stock I && II
- 【leetcode】Array——Best Time to Buy and Sell Stock I/II/III
- leetcode_[python/C++]_121/122/123/188.Best Time to Buy and Sell Stock I/II/III/IV
- 【LeetCode】 Best Time to Buy and Sell Stock I II III IV 解题报告
- [leetcode]Best Time to Buy and Sell Stock I, II, III
- [Leetcode] Best Time to Buy and Sell Stock I,II,III,IV