> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Best Time to Buy and Sell Stock with Cooldown

> Tested Python solution for LeetCode 309 with 14 pytest cases. Generate a practice environment with lcpy.

LeetCode 309, Medium. Topics: Array, Dynamic Programming. [View on LeetCode](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/description/).

Generate this problem as a practice environment: tested reference solution, 14 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 309   # by problem number
lcpy gen -s best_time_to_buy_and_sell_stock_with_cooldown   # by problem name
```

## Problem

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

```
Input: prices = [1,2,3,0,2]
Output: 3
```

**Explanation:** transactions = \[buy, sell, cooldown, buy, sell]

```
Input: prices = [1]
Output: 0
```

### Constraints

* 1 \<= prices.length \<= 5000
* 0 \<= prices\[i] \<= 1000

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/best_time_to_buy_and_sell_stock_with_cooldown/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/best_time_to_buy_and_sell_stock_with_cooldown/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def max_profit(self, prices: list[int]) -> int:
        # State machine: held (own stock), sold (just sold -> cooldown), reset (no stock)
        held = float("-inf")
        sold = float("-inf")
        reset = 0
        for price in prices:
            prev_held, prev_sold, prev_reset = held, sold, reset
            held = max(prev_held, prev_reset - price)
            reset = max(prev_reset, prev_sold)
            sold = prev_held + price
        return int(max(sold, reset))
```

## Complexity

| Time | Space |
| ---- | ----- |
| O(n) | O(1)  |

## Tags

[NeetCode 150](/catalog/neetcode-150), [NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode).
