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]

Analysis

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

Code

public class Solution {
    public int maxProfit(int[] prices) {
        if (prices==null || prices.length==0) return 0;
        int len = prices.length;

        int[] s0 = new int[len];
        int[] s1 = new int[len];
        int[] s2 = new int[len];

        //initialize
        s0[0]=0; 
        s1[0]=-prices[0]; 
        s2[0]=Integer.MIN_VALUE;

        //transfer 
        for (int i=1; i<len; i++){
            s0[i]=Math.max(s0[i-1], s2[i-1]);
            s1[i]=Math.max(s1[i-1], s0[i - 1] - prices[i]);
            s2[i]=s1[i - 1] + prices[i];
        }

        return Math.max(s0[len-1], s2[len-1]);
    }
}

Node

  • question: base case (made to lower value since max is used in transfer)?

Reference

Leetcode