← Whiteboard questions

Best Time to Buy and Sell Stock with Cooldown

Find the best profit when each sale forces a one-day wait

DSA, Medium, 2-D DP

Which structure or algorithm, and what does it cost.

A list prices holds the price of one stock on each day. You may hold at most one share. Each day you may buy one share if you hold none, sell the share you hold, or do nothing. After a sale, you must wait one day, a cooldown day, before you may buy again. Return the largest total profit. You may make no trade, and the profit is then 0.

Examples

Input prices = [4, 2, 5, 3, 8]
Output 6
Why buy at 2 and sell at 8. Selling at 5 first earns 3, makes the day with price 3 a cooldown day, and leaves only the last day, which has nothing to gain.
Input prices = [5, 4, 3, 2]
Output 0
Why prices only fall, so every trade loses money.
Input prices = []
Output 0
Why there are no days, so there is no trade.

Constraints

Your answer must contain

Target Aim for O(n) time and O(1) memory.

Hints

Try the problem first. Open one hint at a time.

Hint 1 a nudge

At the end of any day you are in one of a few situations. Name them, and ask what the best profit is for each.

Hint 2 the approach

Three situations cover it: holding a share, just sold today, and free to buy. Each one comes from the situations of the day before.

Hint 3 the key step

Hold comes from yesterday's hold, or from yesterday's free minus today's price. Work out sold and free the same way, so that no purchase can start from sold.

Walkthrough

Full walkthrough brute force to best

Brute force

Try every plan. Each day you have two choices: buy or wait if you hold nothing, sell or wait if you hold a share. That gives up to 2^n plans (fewer when a cooldown day removes a choice), so it only works for about 20 days.

Better method

Recursion with memory. Let profit(day, state) be the best profit from this day on, where the state is holding a share, free to buy, or in cooldown. Each call looks at the next day. Store each answer. There are 3 * n pairs, so this is O(n) time and memory.

Best method

Track three best profits at the end of each day. hold is the best profit while holding a share. sold is the best profit if you sold today, so tomorrow is a cooldown day. free is the best profit holding nothing and allowed to buy tomorrow. For a new price p, the new hold is the larger of hold and free - p, so a purchase only starts from free. The new sold is hold + p. The new free is the larger of free and sold, because a sale yesterday has become a free day today. The cooldown is built in: buying never starts from sold. The answer is the larger of sold and free at the end. Each value uses only the day before, so three numbers replace the whole table.

Hand trace

Run on [4, 2, 5, 3, 8]. Start with hold -inf, sold 0, free 0. Price 4: hold -4, sold -inf, free 0. Price 2: hold -2, sold -2, free 0. Price 5: hold -2, sold 3, free 0. Price 3: hold -2, sold 1, free 3. Price 8: hold -2, sold 6, free 3. The answer is the larger of sold and free, 6.

Code

def max_profit_cooldown(prices):
    hold, sold, free = float('-inf'), 0, 0   # best profit holding / just sold today / free to buy
    for p in prices:
        hold, sold, free = max(hold, free - p), hold + p, max(free, sold)
    return max(sold, free)

Time and memory

O(n) time, one update per day. O(1) memory, three numbers.

Common mistakes

  • Letting a purchase start from sold. The cooldown day is then ignored, and example 1 gives 8 instead of 6.
  • Starting hold at 0. That pretends a share was held for free, so the profit can be too high.
  • Returning only free. The best plan may end with a sale on the last day, which is in sold.

Related problems

More 2-D DP problems
Best Time to Buy and Sell Stock with Cooldown walkthrough