LeetCode: Best Time to Buy and Sell Stock III [123]
【称号】
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).
【题意】
给定一个数组prices, prices[i]表示第i天的股价。本题规定最多仅仅能买卖两次,问最大收益是多少
【思路】
分别计算买卖一次的最大收益maxProfit1和买卖2次的最大收益maxProfit2,然后求最大值。
买卖一次的解法已经有了,详见Best Time to Buy and Sell Stock。
买卖两次的话我们须要确定转折点在什么地方。即在第i天卖出,在第i+1天买入。为了得到最大值我们须要知道我在第i天卖出的最大收益是多少,在第i+1天买入的最大收入是多少。 求每天卖出可获得的最大收益Best Time to Buy and Sell Stock中已经给出解法,仅仅须要one-pass就完毕。那么怎么计算每天买入可获得的最大收益呢?一样的,仅仅只是换了一个方向而已。
为此我们维护两个数组buyProfit, sellProfit, sellProfit[i]表示在第i天卖出能够获得最大收益。buyProfit[i]表示在第i天买入可获得最大收入。则两次买卖的最大收益maxProfit2=max(buyProfit[i]+sellProfit[i+1]) i=1,2,3,....n-3, 当中n为prices数组的长度。
【代码】
class Solution {
public:
int maxProfit(vector<int> &prices) {
int size=prices.size();
if(size<=1)return 0;
int*back=new int[size];
int*front=new int[size];
int maxProfit=0;
int minPrice=prices[0];
int maxPrice=prices[size-1];
back[size-1]=front[0]=0;
// 求出以i结尾的前半段区间上买卖一次可获得最大收益
maxProfit=0;
for(int i=1; i<size; i++){
int profit=prices[i]-minPrice;
if(profit>maxProfit)maxProfit=profit;
front[i]=maxProfit;
if(prices[i]<minPrice)minPrice=prices[i];
}
// 求出以i開始的后半段区间上买卖一次可获得最大收益
maxProfit=0;
for(int i=size-2; i>=0; i--){
int profit=maxPrice-prices[i];
if(profit>maxProfit)maxProfit=profit;
back[i]= maxProfit;
if(prices[i]>maxPrice)maxPrice=prices[i];
}
//求两次买卖的最大值
maxProfit=0;
for(int i=0; i<size; i++){
if(i==size-1){
if(front[i]>maxProfit)maxProfit=front[i];
}
else{
if(front[i]+back[i+1]>maxProfit)maxProfit=front[i]+back[i+1];
}
}
return maxProfit;
}
};
版权声明:本文博客原创文章,博客,未经同意,不得转载。
LeetCode: Best Time to Buy and Sell Stock III [123]的更多相关文章
- LeetCode: Best Time to Buy and Sell Stock III 解题报告
Best Time to Buy and Sell Stock IIIQuestion SolutionSay you have an array for which the ith element ...
- [LeetCode] Best Time to Buy and Sell Stock III 买股票的最佳时间之三
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] Best Time to Buy and Sell Stock III
将Best Time to Buy and Sell Stock的如下思路用到此题目 思路1:第i天买入,能赚到的最大利润是多少呢?就是i + 1 ~ n天中最大的股价减去第i天的. 思路2:第i天买 ...
- [Leetcode] Best time to buy and sell stock iii 买卖股票的最佳时机
Say you have an array for which the i th element is the price of a given stock on day i. Design an a ...
- [leetcode]Best Time to Buy and Sell Stock III @ Python
原题地址:https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ 题意: Say you have an array ...
- leetcode -- Best Time to Buy and Sell Stock III TODO
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- LeetCode——Best Time to Buy and Sell Stock III
Description: Say you have an array for which the ith element is the price of a given stock on day i. ...
- LeetCode——Best Time to Buy and Sell Stock III (股票买卖时机问题3)
问题: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- LeetCode OJ--Best Time to Buy and Sell Stock III
http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ 这三道题,很好的进阶.1题简单处理,2题使用贪心,3题使用动态 ...
随机推荐
- Boost下载安装编译配置使用指南(含Windows和Linux
理论上,本文适用于boost的各个版本,尤其是最新版本1.45.0:适用于各种C++编译器,如VC6.0(部分库不支持),VS2003,VS2005,VS2008,gcc,C++ Builder等.先 ...
- jQuery事件大全
jQuery事件大全 attribute: $(" p" ).addclass(css中定义的样式类型) 给某个元素添加样式 $(" img" ).attr( ...
- XMPPclient库Smack 4.0.6一个版本的开发
XMPPclient库Smack 4.0.6版开发之中的一个 作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chszs 一.Smack库概述 Smack是一个开源. ...
- 如何在github中贡献自己的力量
如何参与github的开源项目? 1.找一个发出“pull requests”的项目.有以下几个方法:最简单的方式是,读项目的readme文件.它会告诉你,项目的拥有者是否急切的需要协助.如果read ...
- STM8S 串口应用 UART2 STM8S105
//少说话.多做事,下面是我验证过没有问题的串口发送接受数据 //使用MCU stm8s105c6 UART2 //初始化时调用: GPIO_DeInit(GPIOD); /* Configure P ...
- 一起talk C栗子吧(第十二回:C语言实例--单链表一)
各位看官们,大家好.从今天開始,我们讲大型章回体科技小说 :C栗子.也就是C语言实例.闲话休提, 言归正转. 让我们一起talk C栗子吧! 看官们,上一回中咱们没有说详细的样例,并且是说了样例中的文 ...
- Android改变系统自带环形ProgressBar的大小
MainActivity如下: package cc.testprogressbar; import android.os.Bundle; import android.app.Activity; / ...
- HOJ 2245 浮游三角胞(数学啊 )
题目链接:http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=2245 Time Limi ...
- visio ppt axure AI svg powerdesign xmind
visio ppt axure AI svg powerdesign xmind
- vc笔记六
通知消息(Notification message)是指这样一种消息,一个窗口内的子控件发生了一些 事情,需要通 知父窗口.通知消息只适用于标准的窗口控件如按钮.列表框.组合框.编辑框,以及 Wind ...