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题使用动态 ...
随机推荐
- 在DLL中封装的VCL窗体Tab键响应的问题
在DLL中的子窗体不会响应Tab按键的,这个时候就需要手动去指定Tab键的操作,但是前提是主窗体要向这个窗体发送一个消息,一个Tab键按下的消息.基本顺序是这样的: 1. 主窗体用Hook技术捕获Ta ...
- Delphi XE2 升级项目编译ralease版本,无法添加UAC解决方法
我今天把一个原来是Delphi2007的工程升级到了Delphi XE2,在编译ralease版本时候,发现无法添加UAC,我觉得可能是升级的原因,随后我用XE2新建了一个工程,但是在编译raleas ...
- jquery控制动态生成的gridview中多列checkbox的全选反选及自动判断是否全选状态
动态生成的Gridview的前台html代码如下: <table class="usertableborder" cellspacing="0" ...
- Passenger/Nginx/Debian快速部署Rails
安装所需的linux包 sudo apt-get install build-essential bison openssl libreadline6 libreadline6-dev curl gi ...
- Linux学习记录--匿名沟通渠道
匿名沟通渠道 管道Linux最初支持Unix IPC其中的一种形式.具有下列特征: 1.管道是半双工.数据可以仅在一个方向流动:当双方需要沟通.建设两条管线需要. 2.仅仅能用于父子进程或者兄弟进程之 ...
- [Android学习笔记]Android向下兼
Android向下兼容的思路:使用高版本的API,在运行时判断真实运行平台的API版本,根据不同版本做不同的处理 关键类:Build.class里面定义了API版本相关信息 内部类:VERSION定义 ...
- Swift - 操作SQLite数据库(引用SQLite3库)
SQLite轻量级数据库在移动应用中使用非常普遍,但是目前的库是C编写的,为了方便使用,对SQLite相关的操作用Swift进行了封装.这个封装代码使用了一个开源项目SQLiteDB,地址是:http ...
- js错误 SyntaxError: missing : after property id
在用jquery的post方法时 $.post('adminCheckTpmisPlans.do',{'test',str},function(f){ ... }) 报如下错误 SyntaxError ...
- django中mysql数据库设置错误解决方法
刚在django中settings.py进行设置mysql数据库. 当进行执行python manage.py shell命令时会报以下错误: 只需要在settings.py中 DATABASES = ...
- blend
看着各位大虾出系列文章貌似挺好玩的,本人耍了2个月的Wpf,有点见解,希望各位看官笑纳.本系列第一章就先来点简单又有用的吧o(∩_∩)o 哈哈.. 终于效果例如以下: ←点它 本人一直在做WPF算是第 ...