Best Time to Buy and Sell Stock with Cooldown
2017-04-10 17:18
633 查看
Sharemy DP solution (By State Machine Thinking)Hi,I just come across this problem, and it's very frustating since I'm bad at DP.So I just draw all the actions that can be done.Here is the drawing (Feel like an elementary ...)There are three states, according to the action that you can take.Hence, from there, you can now the profit at a state at time i as:
s0[i] = max(s0[i - 1], s2[i - 1]); // Stay at s0, or rest from s2 s1[i] = max(s1[i - 1], s0[i - 1] - prices[i]); // Stay at s1, or buy from s0 s2[i] = s1[i - 1] + prices[i]; // Only one way from s1Then, you just find the maximum of s0and s2, since they will be the maximum profit we need (No one can buy stock and left with more profit that sell right :) )Define base case:
s0[0] = 0; // At the start, you don't have any stock if you just rest s1[0] = -prices[0]; // After buy, you should have -prices[0] profit. Be positive!
s2[0] = INT_MIN; // Lower base caseclass Solution {public:int maxProfit(vector<int>& prices) {if (prices.size() <= 1)return 0;vector<int> s0(prices.size(), 0);vector<int> s1(prices.size(), 0);vector<int> s2(prices.size(), 0);s0[0] = 0;s1[0] = -prices[0];s2[0] = 0;for (int i=1; i<prices.size(); ++i) {s0[i] = max(s0[i-1], s2[i-1]);s1[i] = max(s1[i-1], s0[i-1] - prices[i]);s2[i] = s1[i-1] + prices[i];}return max(s0[prices.size() - 1], s2[prices.size() - 1]);}};
class Solution(object): def maxProfit(self, prices): """ :type prices: List[int] :rtype: int """ if len(prices) <= 1: return 0 s0, s1, s2 = [0]*len(prices), [0]*len(prices), [0]*len(prices) s1[0] = -prices[0] for i in range(1, len(prices)): s0[i] = max(s0[i - 1], s2[i - 1]) s1[i] = max(s1[i - 1], s0[i - 1] - prices[i]) s2[i] = s1[i - 1] + prices[i]
return max(s0[-1], s2[-1])Share my thinking processThe 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:
buy[i] = max(sell[i-2]-price, buy[i-1])sell[i] = max(buy[i-1]+price, sell[i-1])This is better than 3, butwe can do even betterSince states of day
irelies only on
i-1and
i-2we can reduce the O(n) space to O(1). And here we are at our final solution:Java
public int maxProfit(int[] prices) {int sell = 0, prev_sell = 0, buy = Integer.MIN_VALUE, prev_buy;for (int price : prices) {prev_buy = buy;buy = Math.max(prev_sell - price, prev_buy);prev_sell = sell;sell = Math.max(prev_buy + price, prev_sell);}return sell;}C++
int maxProfit(vector<int> &prices) {int buy(INT_MIN), sell(0), prev_sell(0), prev_buy;for (int price : prices) {prev_buy = buy;buy = max(prev_sell - price, buy);prev_sell = sell;sell = max(prev_buy + price, sell);}return sell;}For this problem it is ok to use
INT_MINas initial value, but in general we would like to avoid this. We can do the same as the following python:Python
def maxProfit(self, prices):if len(prices) < 2:return 0sell, buy, prev_sell, prev_buy = 0, -prices[0], 0, 0for price in prices:prev_buy = buybuy = max(prev_sell - price, prev_buy)prev_sell = sellsell = max(prev_buy + price, prev_sell)return sell
相关文章推荐
- Leetcode: Best Time to Buy and Sell Stock with Cooldown
- Leetcode197: Best Time to Buy and Sell Stock with Cooldown
- Leetcode Best Time to Buy and Sell Stock with Cooldown
- Best Time to Buy and Sell Stock with Cooldown
- 309. Best Time to Buy and Sell Stock with Cooldown
- 309. Best Time to Buy and Sell Stock with Cooldown LeetCode
- Best Time to Buy and Sell Stock with Cooldown
- [leetcode 309]Best Time to Buy and Sell Stock with Cooldown--买卖股票with冷却期(DP法)
- leetcode - Best Time to Buy and Sell Stock with Cooldown
- Leetcode: Best Time to Buy and Sell Stock with Cooldown
- leetcode -- Best Time to Buy and Sell Stock with Cooldown -- 不理解
- LeetCode Best Time to Buy and Sell Stock with Cooldown
- LeetCode:Best Time to Buy and Sell Stock with Cooldown
- LeetCode -- Best Time to Buy and Sell Stock with Cooldown
- [LeetCode] Best Time to Buy and Sell Stock with Cooldown 买股票的最佳时间含冷冻期
- leetcode Best Time to Buy and Sell Stock with Cooldown
- [leetcode] Best Time to Buy and Sell Stock with Cooldown
- 【LeetCode从零单刷】Best Time to Buy and Sell Stock I, II, With Cooldown
- 309. Best Time to Buy and Sell Stock with Cooldown
- LeetCode Best Time to Buy and Sell Stock with Cooldown