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

Solution: DP,  caculate max profit both forward and backward, then return the max sum of them.

     int maxProfit(vector<int> &prices) {
if(prices.size() <= )
return ; vector<int> max_forward;
int lowest = prices[];
int max_profit = ;
max_forward.push_back();
for(int i = ; i < prices.size(); i ++ ){
int profit = prices[i] - lowest;
if(profit > max_profit)
max_profit = profit; max_forward.push_back(max_profit);
lowest = min(prices[i], lowest);
} int result = max_forward[max_forward.size() - ];
vector<int> max_backward;
int largest = prices[prices.size() - ];
max_profit = ;
for(int i = prices.size() - ; i >= ; i --) {
int profit = largest - prices[i];
max_profit = max(profit, max_profit); result = max(result, max_profit + max_forward[i]); largest = max(largest, prices[i]);
}
return result;
}

Best Time to Buy and Sell Stock III [LeetCode]的更多相关文章

  1. 123. Best Time to Buy and Sell Stock III ——LeetCode

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

  2. Best Time to Buy and Sell Stock III leetcode java

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

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

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

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

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

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

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

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

随机推荐

  1. ToJson

    /// <summary> /// DataTable转成Json /// </summary> /// <param name="jsonName" ...

  2. HttpClient 与 HtmlParser 简介 转载

    转载地址:https://www.ibm.com/developerworks/cn/opensource/os-cn-crawler/ 本小结简单的介绍一下 HttpClinet 和 HtmlPar ...

  3. Xcode 提高效率的几个快捷键

    1.一次性修改一个scope里的变量名: 点击该变量,出现下划虚线,然后command+control+E激活所有相同变量,然后进行修改. 2.删除一个词:option+delete 删除一句话:co ...

  4. 【jQuery】【转】jQuery中filter()和find()的区别

    Precondition: 现在有一个页面,里面HTML代码为: <div class="css"> <p class="rain">测 ...

  5. 【转】封装Lua for C#

    原文:http://blog.csdn.net/rcfalcon/article/details/5583095 为了搞懂LUA在我们的GDEX中到底怎么用,我决定研究一下如何比较好的在WPF里封装一 ...

  6. Ubuntu-server14.04搭建LAMP环境

    转自:http://www.cnblogs.com/myzhibie/p/4330327.html 对于很多PHP初学开发者来讲,搭建一个可用于生产的LAMP环境是一件费时费力的事情,本文以 ubun ...

  7. log4net一些配置说明

    <configuration> <configSections> <section name="log4net" type="System. ...

  8. 未能解析目标框架“.NETFramework,Version=v4.0”的 mscorlib的解决方法

    本人菜鸟一个,在编码过程中遇到的问题记录下以备忘,高手别笑.最近在做一个项目,公司的VS版本是2010,家里的VS版本是2012.把公司的项目用2012打开后再用2010打开就出现 未能解析目标框架“ ...

  9. css的小技巧

    前几天看到<css揭秘>这本书,第一感觉是 css怎么能出这么厚的一本书,不过 细细一想,用好css真的可以实现很多想要的效果,节省很多js代码. 总结几个css的小技巧: 1,将所有元素 ...

  10. 如何隐藏winform中报表设计器中的按钮

    https://www.devexpress.com/Support/Center/Question/Details/T246117 DesignMdiController.SetCommandVis ...