[LEetCode]Best Time to Buy and Sell Stock with Cooldown
2015-12-06 17:18
489 查看
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 as many transactions as you like (ie, buy one and sell one share of the stock multiple times) with the following restrictions:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day)
Example:
Dp参考讨论区的解法。
The series of problems are typical dp. The key for dp is to find the variables to represent the states and deduce the transition function.
Of course one may come up with a O(1) space solution directly, but I think it is better to be generous when you think and be greedy when you implement.
The natural states for this problem is the 3 possible transactions :
Here
Then the transaction sequences can end with any of these three states.
For each of them we make an array,
is the maxProfit for any sequence end with
is the maxProfit for any sequence end with
is the maxProfit for any sequence end with
Then we want to deduce the transition functions for
By definition we have:
Where
All of these are very straightforward. They simply represents :
One tricky point is how do you make sure you
since from the equations it seems that
Well, the answer lies within the fact that
A further observation is that and
Substitute this in to
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times) with the following restrictions:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day)
Example:
prices = [1, 2, 3, 0, 2] maxProfit = 3 transactions = [buy, sell, cooldown, buy, sell]
Dp参考讨论区的解法。
class Solution { public: int maxProfit(vector<int>& prices) { int pre_buy = INT_MIN; int buy = INT_MIN; int pre_sell = 0; int sell = 0; for(int i=0; i<prices.size();++i){ pre_buy = buy; //pre_buy记录buy[i-1] buy = max(pre_sell-prices[i],buy);//buy计算buy[i],更新buy[i-1]到buy[i] pre_sell = sell; //pre_sell,sell[i-2]更新到sell[i-1]; sell = max(pre_buy+prices[i],sell); //sell计算sell[i],更新sell[i-1]到sell[i] } return sell; } };
The series of problems are typical dp. The key for dp is to find the variables to represent the states and deduce the transition function.
Of course one may come up with a O(1) space solution directly, but I think it is better to be generous when you think and be greedy when you implement.
The natural states for this problem is the 3 possible transactions :
buy,
sell,
rest.
Here
restmeans no transaction on that day (aka cooldown).
Then the transaction sequences can end with any of these three states.
For each of them we make an array,
buy,
selland
rest.
buy[i]means before day
iwhat
is the maxProfit for any sequence end with
buy.
sell[i]means before day
iwhat
is the maxProfit for any sequence end with
sell.
rest[i]means before day
iwhat
is the maxProfit for any sequence end with
rest.
Then we want to deduce the transition functions for
buy
selland
rest.
By definition we have:
buy[i] = max(rest[i-1]-price, buy[i-1]) sell[i] = max(buy[i-1]+price, sell[i-1]) rest[i] = max(sell[i-1], buy[i-1], rest[i-1])
Where
priceis the price of day
i.
All of these are very straightforward. They simply represents :
(1) We have to `rest` before we `buy` and (2) we have to `buy` before we `sell`
One tricky point is how do you make sure you
sellbefore you
buy,
since from the equations it seems that
[buy, rest, buy]is entirely possible.
Well, the answer lies within the fact that
buy[i] <= rest[i]which means
rest[i] = max(sell[i-1], rest[i-1]). That made sure
[buy, rest, buy]is never occurred.
A further observation is that and
rest[i] <= sell[i]is also true therefore
rest[i] = sell[i-1]
Substitute this in to
buy[i]we now have 2 functions instead of 3:
相关文章推荐
- ASP.NET WebForm 的CheckBoxList控件通过js获取Value值问题
- 小王梦游记四----------隐形天使
- Xcode 编辑时的爆炸效果
- 放弃Hexo,改用CSDN
- 第五次实验报告
- PM 3
- 【iOS】UITbableView中cell的单选状态和多选状态
- 段机制
- nyoj 517 最小公倍数 【java睑板】
- 第四次实验报告
- Windows下MySQL双向同步及环形同步的实现
- 如何批量修改文件名后缀、扩展名
- Activity启动模式详解
- Spring 主配置文件详解
- square的leakcanary库实现分析
- Android 动态测量list view item的高度
- Redis安装部署
- 自动筛选符合条件的行数
- 堆的实现及堆的各种应用
- Robot Framework-工具简介及入门使用