题目:

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

解题思路:

话说这题同前两题难度瞬间就拉开好多,哎,编程能力还是不行啊,如果不是谷歌各路大神解题报告http://blog.csdn.net/pickless/article/details/12034365,真心想不出来。

这题实际上用到了DP和分段的思想。

首先,根据题意,要求至少买卖两次(就因为有这限制,使得题目难度突然就增加了),所以,我们可以进行分段。

寻找一个点i,将原来的price[0..n-1]分割为price[0..i]和price[i..n-1],分别求两段的最大profit,可知分段就是使得买卖至少进行两次。

下面求price[0..i]和price[i..n-1]两段的最大profit时,利用了DP思想。

对于点i+1,求price[0..i+1]的最大profit时,很多工作是重复的,在求price[0..i]的最大profit中已经做过了。

类似于Best Time to Buy and Sell Stock,可以在O(1)的时间从price[0..i]推出price[0..i+1]的最大profit。

但是如何从price[i..n-1]推出price[i+1..n-1]?反过来思考,我们可以用O(1)的时间由price[i+1..n-1]推出price[i..n-1]。

最终算法:

数组l[i]记录了price[0..i]的最大profit,

数组r[i]记录了price[i..n]的最大profit。

已知l[i],求l[i+1]是简单的,同样已知r[i],求r[i-1]也很容易。

最后,我们再用O(n)的时间找出最大的l[i]+r[i],即为题目所求。

实现代码:

#include <iostream>
#include <vector>
using namespace std; /**
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). */ class Solution {
public:
int maxProfit(vector<int> &prices) {
if(prices.empty())
return 0;
int n = prices.size();
int *l = new int[n];
int *r = new int[n];
l[0] = 0;
int lmin = prices[0];
for(int i = 1; i < n; i++)
{
lmin = min(prices[i],lmin);
l[i] = max(l[i-1], prices[i] - lmin);
} r[n-1] = 0;
int rmax = prices[n-1];
for(int i = n - 2; i >= 0; i--)
{
rmax = max(rmax, prices[i]);
r[i] = max(r[i+1], rmax - prices[i]);
} int maxprofit = 0;
for(int i = 0; i < n; i++)
{
maxprofit = max(maxprofit, l[i] + r[i]);
}
delete l;
delete r;
return maxprofit; }
}; int main(void)
{
int arr[] = {2,4,5,1,7,10};
int n = sizeof(arr) / sizeof(arr[0]);
vector<int> stock(arr, arr+n);
Solution solution;
int max = solution.maxProfit(stock);
cout<<max<<endl;
return 0;
}

LeetCode123:Best Time to Buy and Sell Stock III的更多相关文章

  1. 27. Best Time to Buy and Sell Stock && Best Time to Buy and Sell Stock II && Best Time to Buy and Sell Stock III

    Best Time to Buy and Sell Stock (onlineJudge: https://oj.leetcode.com/problems/best-time-to-buy-and- ...

  2. LeetCode 笔记23 Best Time to Buy and Sell Stock III

    Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the price of ...

  3. Best Time to Buy and Sell Stock | & || & III

    Best Time to Buy and Sell Stock I Say you have an array for which the ith element is the price of a ...

  4. 【leetcode】Best Time to Buy and Sell Stock III

    Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the price of ...

  5. LeerCode 123 Best Time to Buy and Sell Stock III之O(n)解法

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  6. 【leetcode】123. Best Time to Buy and Sell Stock III

    @requires_authorization @author johnsondu @create_time 2015.7.22 19:04 @url [Best Time to Buy and Se ...

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

  8. [leetcode]123. 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 ...

  9. LN : leetcode 123 Best Time to Buy and Sell Stock III

    lc 123 Best Time to Buy and Sell Stock III 123 Best Time to Buy and Sell Stock III Say you have an a ...

随机推荐

  1. webservice4

    原理: 参考 http://lvwenwen.iteye.com/blog/1478236 客户端通过get请求可以得到wsdl文件,也就知道服务器提供的方法和参数了,然后客户端会通过webservi ...

  2. java.rmi.NoSuchObjectException: no such object in table

    jmx链接的时候,最简单的例子都行不通,郁闷,出现了: 参考:http://reiz6153.blog.163.com/blog/static/401089152009442723208/ 代码: M ...

  3. Atitit usrQBM1603短信验证码规范

    Atitit usrQBM1603短信验证码规范 短信验证码扩展至短信服务和验证码服务1 主要方法1 参考模板1 短信验证码扩展至短信服务和验证码服务 主要方法 Line 27: public cla ...

  4. C#学习系列-this的使用

    如有错误,欢迎指正. 1.代表当前类,在当前类中可使用this访问当前类成员变量和方法(需要注意的是 静态方法中不能使用this),也可用于参数传递,传递当前对象的引用. 下面贴代码: class P ...

  5. C#并行编程-相关概念

    菜鸟初步学习,不对的地方请大神指教,参考<C#并行编程高级教程.pdf> 目录 C#并行编程-相关概念 C#并行编程-Parallel C#并行编程-Task C#并行编程-并发集合 C# ...

  6. fir.im Weekly - Stanford 的 Swift 课程来了

    上周提过,Swift 的 Github 主页上已经有了 >>「Port to Android」,这周重点推荐一下 Stanford 的 Swift 课程. Developing iOS 9 ...

  7. fir.im Weekly - 如何做一个出色的程序员

    做一个出色的程序员,困难而高尚.本期 fir.im Weekly 精选了一些实用的 iOS,Android 开发工具和源码分享,还有一些关于程序员的成长 Tips 和有意思有质量的线下活动~ How ...

  8. Android笔记——Android框架

    本篇将站在顶级的高度--架构,来看android.我开篇就说了,这个系列适合0基础的人且我也是从0开始按照这个步骤来 学的,谈架构是不是有点螳臂挡车,自不量力呢?我觉得其实不然,如果一开始就对整个an ...

  9. CKEditor与CKFinder整合 MVC3

    今天偶然看到一篇关于 CKEditor与CKFinder整合文章,心想有一段时间没有使用这种东西了.于是乎自己动手亲自体验了一下,本以为简单但在东西编写的过程发现了很多没有遇见毛病. 所以记录一下自己 ...

  10. OpenCascade Modeling Algorithms Fillets and Chamfers

    Modeling Algorithms Fillets and Chamfers 造型算法——倒圆与倒角 eryar@163.com 一.倒圆Fillet Constructor 1. BRepFil ...