Skip to main content
LeetCode 309, Medium. Topics: Array, Dynamic Programming. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 14 parametrized pytest cases, and a playground notebook:

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

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

Constraints

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

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

NeetCode 150, NeetCode 250, NeetCode All.
Last modified on August 25, 2026