Skip to main content
  1. LeetCode/

LeetCode 309: Best Time to Buy and Sell Stock with Cooldown

·3 mins· ·
LeetCode Medium Dynamic-Programming State-Machine
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-05-17 Source: Day 23 learning note

Study Context
#

This article is rebuilt from the exact LeetCode section in the learning note. I kept the note’s repair points, comparison points, and common mistakes, while removing unrelated non-LeetCode material from the same day.

Learning Note Extract
#

Problem 1 - LC 309 Best Time to Buy and Sell Stock with Cooldown
#

  • Pattern: state-machine DP with one-day post-sell restriction.

Why This Fits
#

The problem is still:

best profit under exact holding conditions after day i

But now selling changes what is legal on the next day:

after a sell, you cannot buy immediately the next day

That means the simple hold/cash model is not enough unless the cooldown effect is represented explicitly.

Core State / Invariant
#

One clean 3-state version:

hold = best profit after day i while holding one stock
sold = best profit after day i if we sold today
rest = best profit after day i while not holding and not selling today

Meaning matters more than the formula:

  • hold means we own a stock at end of day
  • sold means we just sold today, so tomorrow is cooldown
  • rest means we are free to buy tomorrow

Transitions
#

hold = max(previous_hold, previous_rest - price)
sold = previous_hold + price
rest = max(previous_rest, previous_sold)

Complexity
#

Time: O(n)
Space: O(1)

Common Mistakes
#

  • reusing sold immediately for a same-day buy transition
  • collapsing all non-hold states into one state and losing cooldown meaning
  • memorizing 3 variables without being able to explain what each means
  • returning hold instead of realized-profit states at the end

Strong Spoken Explanation
#

I model the best profit under three end-of-day conditions: holding, sold-today, and resting. Cooldown matters because the day after a sell is not buy-eligible, so a new buy can only come from rest, not from sold. Each transition follows directly from that state meaning, which is why I do not need to memorize the formula.

Final Answer Meaning
#

answer = max(sold, rest)

Reason:

  • final realized profit must be a non-holding state
  • ending in hold means the profit is not fully realized yet

Clean Solution
#

The note above captures the reasoning and the mistakes to avoid. The implementation below is the version I would submit.

from typing import List

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        hold = float('-inf')
        sold = float('-inf')
        rest = 0

        for price in prices:
            hold, sold, rest = max(hold, rest - price), hold + price, max(rest, sold)

        return max(sold, rest)

Complexity
#

Time O(n), Space O(1).

Mistakes To Watch
#

  • Buying immediately after a sell.
  • Using one cash state without modeling cooldown.

Final Interview Explanation
#

Start from the state definition, then explain why the transition preserves that state. If there is a loop direction, state compression, or a similar-looking problem with a different answer shape, call that out explicitly because that is where this problem family usually breaks down.

Related

LeetCode 714: Best Time to Buy and Sell Stock with Transaction Fee
·3 mins
LeetCode Medium Dynamic-Programming State-Machine
LeetCode note for Best Time to Buy and Sell Stock with Transaction Fee, rebuilt from the original learning note
LeetCode 122: Best Time To Buy And Sell Stock II
·2 mins
LeetCode Medium Dynamic-Programming Greedy State-Machine
LeetCode note for Best Time To Buy And Sell Stock II, rebuilt from the original learning note
LeetCode 123: Best Time to Buy and Sell Stock III
·4 mins
LeetCode Hard Dynamic-Programming State-Machine
LeetCode note for Best Time to Buy and Sell Stock III, rebuilt from the original learning note
LeetCode 188: Best Time to Buy and Sell Stock IV
·4 mins
LeetCode Hard Dynamic-Programming State-Machine
LeetCode note for Best Time to Buy and Sell Stock IV, rebuilt from the original learning note
LeetCode 121: Best Time To Buy And Sell Stock
·2 mins
LeetCode Easy Dynamic-Programming State-Machine
LeetCode note for Best Time To Buy And Sell Stock, rebuilt from the original learning note
LeetCode 413: Arithmetic Slices
·3 mins
LeetCode Medium Dynamic-Programming
LeetCode note for Arithmetic Slices, rebuilt from the original learning note