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).
分析: First consider the case that when we are only allowed to make one transaction. we can handle this easily with DP. If we move forward, any new price we meet will only affect our history result by two ways:

will it be so low that it beats our previous lowest price?
will it be so high that we should instead sell on this time to gain a higher profit (than the history record)? Similarly, we can move backward with the highest price and profit in record. Either way would take O(n) time.
Now consider the two transaction case. Since there will be no overlaps, we are actually dividing the whole time into two intervals. We want to maximize the profit in each of them so the same method above will apply here. We are actually trying to break the day at each time instance, by adding the potential max profit before and after it together. By recording history and future for each time point, we can again do this within O(n) time. 摘自 : http://discuss.leetcode.com/questions/287/best-time-to-buy-and-sell-stock-iii?sort=votes&page=1

  

class Solution {
public:
//III
int maxProfit(vector<int> &prices) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int len = prices.size();
if(len < ) return ; int minheight, maxheight;
vector<int> history(len, );
vector<int> future(len, ); minheight = prices[];
history[] = ;
for( int i = ; i< len; ++i){
if(minheight > prices[i])
minheight = prices[i]; history[i] = history[i-] > prices[i]- minheight ? history[i-]
: prices[i] - minheight;
} int res = ;
maxheight = prices[len-];
future[len-] = ;
for( int i = len -; i>= ; --i){
if(maxheight < prices[i])
maxheight = prices[i]; future[i] = future[i+] > maxheight - prices[i] ? future[i+]
: maxheight - prices[i] ; res = res > future[i] + history[i] ? res : future[i]+ history[i];
} return res;
}
};

LeetCode_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. C# winform如何清除由Graphics类绘制出来的所有线条或图形

    在C#winform应用程序中,可以用GDI绘制出线条或图形. 1.在主窗体上绘制线条或图形 using (Graphics g = this.CreateGraphics())      {    ...

  2. Android ListView嵌套Button,Button事件覆盖item事件解决办法

    方法就是修改item布局的xml文件: 在根布局里加上: android:descendantFocusability="blocksDescendants" 然后在按钮布局里加上 ...

  3. 51nod-正整数分组问题(基础方程DP-01背包)

    正整数分组 将一堆正整数分为2组,要求2组的和相差最小. 例如:1 2 3 4 5,将1 2 4分为1组,3 5分为1组,两组和相差1,是所有方案中相差最少的. 思路: 这题的实质其实也是0-1背包问 ...

  4. weblogic迁移随手记

    新建域的脚本weblogic  登录缓慢监听地址的修改,hosts修改vim +/securerandom /usr/java/jdk1.7.0_71/jre/lib/security/java.se ...

  5. iOS--创建uiscrollview

    //创建uiscrollview self.PageHeight = self.view.bounds.size.height; self.PageWidth = self.view.bounds.s ...

  6. HTML标准事件(包含HTML5)

    参考菜鸟教程链接:http://www.runoob.com/tags/ref-eventattributes.html

  7. Heritrix源码分析(十四)

    近段时间在搞定Lucene的一些问题,所以Heritrix源码分析暂时告一段落.今天下午在群里有同学提到了Heritrix异常终止的问题以及让Heritrix不停的抓取(就是抓完一遍后载入种子继续抓取 ...

  8. ios8中百度推送接收不到

    ios8中百度推送接收类型会有所改变: //消息推送注冊 if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) { ...

  9. swift 中String常用操作

    1.  字符串定义 var s = "aaaaaa" // 两个字符串均为空并等价. var emptyString = ""   var anotherEmp ...

  10. MySQL加密的性能测试

    这是对MySQL进行加密性能测试的两篇文章系列之二.在第一篇中,我专门使用MySQL的内置的对SSL的支持来 做压力测试,产生了一些令人惊讶的结果. AD:WOT2015 互联网运维与开发者大会 热销 ...