【称号】

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]的更多相关文章

  1. 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 ...

  2. [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 ...

  3. [LeetCode] Best Time to Buy and Sell Stock III

    将Best Time to Buy and Sell Stock的如下思路用到此题目 思路1:第i天买入,能赚到的最大利润是多少呢?就是i + 1 ~ n天中最大的股价减去第i天的. 思路2:第i天买 ...

  4. [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 ...

  5. [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 ...

  6. 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 ...

  7. 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. ...

  8. 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 ...

  9. 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题使用动态 ...

随机推荐

  1. 基于visual Studio2013解决C语言竞赛题之1094纵横图

        题目 解决代码及点评 /************************************************************************/ /* ...

  2. 使用Material Design 创建App翻译系列----材料主题的使用(Using Material Theme)

    上一篇是使用Material Design 创建App翻译系列--開始学习篇,进入正题: 新的材料主题提供了下面内容: 1. 提供了同意设置颜色板的系统部件组件. 2. 为这些系统组件提供了触摸反馈动 ...

  3. 在Qt中使用ActiveX控件

    Qt的windows商业版本提供了ActiveQt这个framework,使用这个组件我们可以在Qt中使用ActiveX控件,并且也开发基于Qt的ActiveX控件.ActiveQt包含了两个组件QA ...

  4. c friend -- 友元

    c friend -- 友元 友元用于突破protected 或者 private 保护的限制,首先要做的是在被访问者的类中声明是友元函数或者友元类.代码如下 #include <iostrea ...

  5. HDU1849 Rabbit and Grass()

    用异或看取得的值是否为0推断 思想换没搞懂 #include<stdio.h> int main() { int ans,n,a; while(scanf("%d",& ...

  6. 【Error】JavaWeb: 严重: Failed to initialize end point associated with ProtocolHandler [&quot;http-bio-8080&quot;]

    在MyEclipse中启动Tomcat时出现错误,错误信息例如以下: 严重: Failed to initialize end point associated with ProtocolHandle ...

  7. E. Mike and Foam(容斥原理)

    E. Mike and Foam Mike is a bartender at Rico's bar. At Rico's, they put beer glasses in a special sh ...

  8. mysql+ssh整合样例,附源代码下载

    项目引用jar下载:http://download.csdn.net/detail/adam_zs/7262727 项目源代码下载地址:http://download.csdn.net/detail/ ...

  9. Codeforce 143B - Help Kingdom of Far Far Away 2

    B. Help Kingdom of Far Far Away 2 time limit per test 2 seconds memory limit per test 256 megabytes ...

  10. 使用KnockoutJs+Bootstrap实现分页

    [后端人员耍前端系列]KnockoutJs篇:使用KnockoutJs+Bootstrap实现分页   一.引言 由于最近公司的系统需要改版,改版的新系统我打算使用KnockoutJs来制作Web前端 ...