Problem Definition

You are given an array prices where prices[i] is the price of a given stock on the ith day.

Find the maximum profit you can achieve. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the following restrictions:

  • After you sell your stock, you cannot buy stock on the next day (i.e., cooldown one day).

Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).

Examples

Example 1:

Input: prices = [1,2,3,0,2]
Output: 3
Explanation: transactions = [buy, sell, cooldown, buy, sell]

Example 2:

Input: prices = [1]
Output: 0

Solution

Method 1 - Using State Machine and DP

We have already seen this approach in - Best Time To Buy And Sell Stock 4 - at most k times. Lets try to thik on similar lines.

The natural states for this problem are the three possible actions: buysell, and rest (no transaction/cooldown).

Each transaction sequence can end in one of these states. For each, we define arrays: buy[n]sell[n], and rest[n].

  • buy[i]: Maximum profit with a sequence ending in buying on day i.
  • sell[i]: Maximum profit with a sequence ending in selling on day i.
  • rest[i]: Maximum profit with a sequence ending in resting on day i.

Then we want to deduce the transition functions for buy sell and rest. The transitions are defined as:

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

Here, price is the price on day i. Essentially, this implies:

(1) Rest before we buy
(2) Buy before we sell

A key point is ensuring that you always sell before you buy again. Since buy[i] <= rest[i], which means rest[i] = max(sell[i-1], rest[i-1]), it ensures that [buy, rest, buy] does not occur.

Additionally, since rest[i] <= sell[i], it follows that:

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

We can further reduce the space complexity from O(n) to O(1) as each day’s state only depends on the previous one or two days:

Code

Java
public int maxProfit(int[] prices) {
    int sell = 0, prevSell = 0, buy = Integer.MIN_VALUE, prevBuy;
    for (int price : prices) {
        prevBuy = buy;
        buy = Math.max(prevSell - price, prevBuy);
        prevSell = sell;
        sell = Math.max(prevBuy + price, prevSell);
    }
    return sell;
}

Method 2 - Just Look at Selling and Cooldown

Code

Java
public int maxProfit(int[] prices) {
	if(prices == null || prices.length < 2){
		return 0;
	}
	int n = prices.length;
	int[] sell = new int[n]; //sell[i] means must sell at day i
	int[] cooldown = new int[n]; //cooldown[i] means day i is cooldown day
	sell[1] = prices[1] - prices[0];
	for(int i = 2; i < prices.length; ++i){
		cooldown[i] = Math.max(sell[i - 1], cooldown[i - 1]);
		sell[i] = prices[i] - prices[i - 1] + Math.max(sell[i - 1], cooldown[i - 2]);
	}
	return Math.max(sell[n - 1], cooldown[n - 1]); 
}