Skip to content

Latest commit

 

History

History
109 lines (87 loc) · 3.27 KB

309.md

File metadata and controls

109 lines (87 loc) · 3.27 KB
  1. Best Time to Buy and Sell Stock with Cooldown
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:

prices = [1, 2, 3, 0, 2]
maxProfit = 3
transactions = [buy, sell, cooldown, buy, sell]
Credits:
Special thanks to @dietpepsi for adding this problem and creating all test cases.

my thoughts:

1. for each day calculate the max gain for both sell on that day or not sell on that day.
time: O(n^2); space: O(n)
TLE

my solution:

********** TLE
class Solution:
    def maxProfit(self, prices):
        """        
        :type prices: List[int]
        :rtype: int
        """
        if not prices or len(prices) < 2:
            return 0
        l = len(prices)
        sell = [0] * (l + 1)
        noSell = [0] * (l + 1)
        sell[2] = prices[1] - prices[0]
        i = 3
        while i < l + 1:
            cur = 0
            for j in range(i - 1):
                profit = prices[i - 1] - prices[j]
                prev = max(sell[j - 1], noSell[j - 1]) if j > 2 else 0
                if profit + prev > cur:
                    cur = profit + prev
                #print(i, j, cur)
            sell[i] = cur
            noSell[i] = max(sell[i - 1], noSell[i - 1])
            i += 1
        
        return max(sell[-1], noSell[-1])

my comments:


from other ppl's solution:

1. https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/discuss/75928/Share-my-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 …)



enter image description here

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 s1
Then, you just find the maximum of s0[n] and s2[n], 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 case
Here is the code :D

class 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);
		s1[0] = -prices[0];
		s0[0] = 0;
		s2[0] = INT_MIN;
		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]);
	}
};