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. java调优随记-堆和栈

    基础知识: 关于堆和栈,堆和栈是程序运行的关键,关于堆和栈的定义和解释可自行搜索,我比较认可以程序运行过程中他们扮演的角色作为对比的点:堆是存储的单位,而栈是程序运行时的单位.栈解决的是程序的运行问题 ...

  2. ViewPager的使用方法和实现过程

    布局文件里添加viewPager布局 <android.support.v4.view.ViewPager android:id="@+id/search_viewpager" ...

  3. MongoDB开发学习(1)开天辟地,经典入门

    原文地址:http://www.cnblogs.com/xumingxiang/archive/2012/04/08/2437468.html 如果你从来没有接触MongoDB或对MongoDB有一点 ...

  4. HDU_2029——回文串的判断

    Problem Description “回文串”是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串.请写一个程序判断读入的字符串是否是“回文”.   Input 输入包 ...

  5. cocos2d-x 几何绘制: DrawingPrimitives 和 CCDrawNode

    在看书的时候只提到了DrawingPrimitives,然后我去搜索这个类,结果没搜到.心想难道是类名改了,那我搜方法名吧,搜了下DrawLine,果然被我搜到了.结果发现原来这些各方法都是全局函数, ...

  6. HDU5029--Relief grain (树链剖分+线段树 )

    题意:n个点构成的无根树,m次操作, 对于操作 x y z, 表示 x 到 y 路径上的 每个点 加一个 z 数字,可重复加.最后输出每个点 加的次数最多的那个数字,如果没有输出0. 赤裸裸的树链剖分 ...

  7. Java使用poi包读取Excel文档

    项目需要解析Excel文档获取数据,就在网上找了一些资料,结合自己这次使用,写下心得: 1.maven项目需加入如下依赖: <dependency> <groupId>org. ...

  8. linux下mysql的卸载、安装全过程

    卸载mysql 1.查找以前是否装有mysql 命令:rpm -qa|grep -i mysql 可以看到mysql的两个包: mysql-4.1.12-3.RHEL4.1 mysqlclient10 ...

  9. android中定位光标位置

    edittext.setSelection(int); edittext.setText(123);//设置edittext中的内容 edittext.setSelection(123.length( ...

  10. linux服务器内存占用太高-释放内存

    修改/proc/sys/vm/drop_caches,释放Slab占用的cache内存空间(参考drop_caches的官方文档): Writing to this will cause the ke ...