lc 123 Best Time to Buy and Sell Stock III


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

DP Accepted

题目要求最多只能交易(买进卖出)两次。所以就可以用sell2[i]表示前i天第二次卖出后手中最多有的钱,buy2[i]表示前i天第二次买入后手中最多有的钱,sell1[i]表示前i天第一次卖出后手中最多有的钱,buy1[i]表示前i天第一次买入后手中最多有的钱,其中,假设一开始手中钱的数量为0。可以得到规律:

sell2[i] = max(sell2[i-1], buy2[i-1]+prices[i]);

buy2[i] = max(buy2[i-1], sell1[i-1]-prices[i]);

sell1[i] = max(sell1[i-1], buy1[i-1]+prices[i]);

buy1[i] = max(buy1[i-1], -prices[i]);

观察可以发现,上述四个变量的值只与前一状态有关,所以可以用单个的变量来代替数组。

class Solution {
public:
int maxProfit(vector<int>& prices) {
int sell2 = 0, sell1 = 0, buy2 = numeric_limits<int>::min(), buy1 = numeric_limits<int>::min();
for (int i = 0; i < prices.size(); i++) {
sell2 = max(sell2, buy2+prices[i]);
buy2 = max(buy2, sell1-prices[i]);
sell1 = max(sell1, buy1+prices[i]);
buy1 = max(buy1, -prices[i]);
}
return sell2;
}
};

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

  1. [LeetCode] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 III

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

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

  3. Java for LeetCode 123 Best Time to Buy and Sell Stock III【HARD】

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

  4. leetcode 123. Best Time to Buy and Sell Stock III ----- java

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

  5. LeetCode 123. Best Time to Buy and Sell Stock III (stock problem)

    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

    原题地址 最直观的想法就是划分成两个子问题,每个子问题变成了:求在某个范围内交易一次的最大利润 在只能交易一次的情况下,如何求一段时间内的最大利润?其实就是找股价最低的一天买进,然后在股价最高的一天卖 ...

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

  8. 【刷题-LeetCode】123 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 ...

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

随机推荐

  1. 【JS】JavaScript引擎的内部执行机制

     近期在复习JavaScript,看到setTimeout函数时.想起曾经刚学时,在一本书上看过setTimeout()里的回调函数执行的间隔时间有昌不是后面设置的值.曾经没想太多.网上看了JS大 ...

  2. WHU-1551-Pairs(莫队算法+分块实现)

    Description Give you a sequence consisted of n numbers. You are required to answer how many pairs of ...

  3. POJ 1936 All in All(串)

    All in All Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 27537 Accepted: 11274 Descript ...

  4. hdu4737A Bit Fun 线段树

    //给一串序列,问有多少对[i,j]使得 //[i,j]区间的全部数的或的值小于m //能够知道'或'操作的加(a|b)>=max(a,b) //能够枚举区间的右边r,找左边第一个不满足的位置 ...

  5. flex集成IFrame,IFrame集成UnityWebPlayer直接通讯调用解决方式

    做Web开发一般是flex与JS交互,UnityWebPlayer与JS交互. 它们之间相互调用比較常见. /** * Flex调用Javascript函数 * @params functionNam ...

  6. 错误 1 无法将程序集“NBear.Data.dll”复制到文件“D:\newbpm\bpm\SureBpm\Bin\NBear.Data.dll”。无法将“D:\newbpm\bpm\SureSoft.WebServiceBaseLib\bin\Debug\NBear.Data.dll”添加到网站。 无法添加文件“Bin\NBear.Data.dll”。 拒绝访问。 D:\..

    错误 1 无法将程序集“NBear.Data.dll”复制到文件“D:\newbpm\bpm\SureBpm\Bin\NBear.Data.dll”.无法将“D:\newbpm\bpm\SureSof ...

  7. 什么是 XML Schema?

    XML Schema 的作用是定义 XML 文档的合法构建模块,类似 DTD. XML Schema: 定义可出现在文档中的元素 定义可出现在文档中的属性 定义哪个元素是子元素 定义子元素的次序 定义 ...

  8. Struts2自定义过滤器的小例子-入门篇

    创建web项目    实现的效果! 用户点击页面不同的链接,后台调用不同的代码! 创建两个类实现共同的接口! public interface Action { String execute(); } ...

  9. mysql16---读写分离

    读写分离(负载平衡)(读写分离肯定要用到主从复制) 如果数据库压力很大,一台机器支撑不了,那么可以用mysql复制实现多台机器同步,将数据库的压力分散. 分表不能解决并发量大的问题. Sql语句发过来 ...

  10. linux多线程编程入门

    背景知识: 在前一个实训中我们介绍了进程,但有时人们认为用fork调用来创建新进程的代价太高.在这种情况下,如果能让一个进程同时做零件事情或至少看起来是这样将会非常有用.而且,你可能希望能有两件或更多 ...