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. web 开发之js---ajax 异步处理

    本文介绍了如何创建能够适应不同浏览器的XMLHttpRequest实例,建立和发送请求,并响应服务器.您将开始接触最基本和基础性的有关Ajax的全部对象和编程方法:XMLHttpRequest对象.该 ...

  2. VC2010 利用 def 文件生成 dll 文件的方法

    近期有个需求,要生成一个dll 文件.文件里的函数都是採用 stdcall 函数调用约定,可是不希望函数名被修饰(add 被修饰成 add@8). 这时就要用def 文件了. 比方我有以下两个函数: ...

  3. select标签multiple属性的用法

    前些日子公司让做一个功能模块.对于里面一个小功能费了些周折,现将其总结一下: 一.实现效果: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZ2FvaHVh ...

  4. bzoj1833: [ZJOI2010]count 数字计数&&USACO37 Cow Queueing 数数的梦(数位DP)

    难受啊,怎么又遇到我不会的题了(捂脸) 如题,这是一道数位DP,随便找了个博客居然就是我们大YZ的……果然nb,然后就是改改模版++注释就好的了,直接看注释吧,就是用1~B - 1~A-1而已,枚举全 ...

  5. YTU 1011: Rails

    1011: Rails 时间限制: 1000 Sec  内存限制: 64 MB 提交: 16  解决: 9 题目描述 There is a famous railway station in PopP ...

  6. fcitx-configtool

    配置输入法

  7. Masonry自动布局:复合约束

    前言 说到iOS自动布局,有很多的解决办法.有的人使用xib/storyboard自动布局,也有人使用frame来适配.对于前者,笔者并不喜欢,也不支持.对于后者,更是麻烦,到处计算高度.宽度等,千万 ...

  8. [Usaco2017 Dec] A Pie for a Pie

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=5140 [算法] 最短路 时间复杂度 : O(N^2) [代码] #include&l ...

  9. 【HAOI 2008】 硬币购物

    [题目链接] 点击打开链接 [算法] 此题是一道好题! 首先,我们发现 : 付款方法数 = 不受限制的方法数 - 受限制的方法数 那么,我们怎么求呢? 我们用dp求出不受限制的方法数(f[i]表示买i ...

  10. cookie绕过验证码登录

    #coding:utf-8 ''' cookie绕过验证码登录,第一步先访问登录页面获取登录前的cookie,第二步用fiddler抓到的手动登录的cookie加入cookie中,登录成功,第三步登录 ...