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的MDI子窗体
在DLL动态链接库中封装VCL的MDI子窗体不多说了,看代码就应该明白了,曾经我遇到的问题,现在放出来大家共享! 这里是工程文件的部分: 在DLL中封装MDI子窗体需要重写DLL入口函数,具体代码如下 ...
- spring mvc controller json数据
项目中遇到个批处理,需要前台传递一个json格式对象数组,如下:var data={ "wos":[{"id":1,"satisfaction&q ...
- 解决删除Volume报错的问题
很久没有遇到过删除Volume出错使得Volume处于Error_Deleting状态的情况了,昨天删除一个Volume时又出现了这个问题,这里顺便把解决方法记录一下. 注意我这里针对的是后端采用is ...
- 从头学起android<AudioManager 声音编辑器.五十.>
我们用android经常使用的时候,手机的声音增大和缩小操作.在设定场景模式,它往往使用静音和振动运行,这通常是AudioManager来控制的. 今天我们就来看一下AudioManager 的使用. ...
- vc中改变对话框的背景色
---- 笔者曾在<软件报>2000年第5期中讨论过如何改变控件的颜色,但还有相当一部分的读者来信提问:一个基于对话框的MFC AppWizard应用程序中,如何改变对话框的背景颜色呢?对 ...
- 使用iftop网络流量监控
iftop这是一个非常有用的工具.下面的命令监视无线网卡在我的笔记本 iftop -i wlan0 比如,我现在玩音乐视频.iftop显示的信息: 基本说明: 1. 屏幕主要部分都是表示两个机器之间的 ...
- MySQL如何修改root密码
MySQL修改用户密码 因为长期不登录MySQL数据库,登录时经常忘记root权限密码.本文提供一个在数据库服务器上修改root密码的方法,本文撰写基础是在xp操作系统下进行. 第一步 ...
- go 冒泡排序
package main import ( "fmt" ) func main() { a := [...], , , , , , , , , } num := len(a) fm ...
- cannot run program "git.exe":CreateProcess error=2
在使用android studio从git上check项目的时候报错cannot run program "git.exe":CreateProcess error=2 请检查下面 ...
- android在Canvas使用drawBitmap画一幅画
1.画图的主要方法 //Bitmap:图片对象,left:向左偏移.top: 顶部偏移 drawBitmap(Bitmap bitmap, float left, float top, Pai ...