InterviewPrepKit

Home / Coding / Algorithm & Data Structure / 2-D Dynamic Programming

Best Time to Buy and Sell Stock with Cooldown

medium Original ↗ 00:00

Problem

You are given prices, where prices[i] is the price of one share on day i. You may complete as many buy/sell transactions as you like, but with two rules:

  • You may hold at most one share at a time (you must sell before buying again).
  • After you sell, you must wait a full cooldown day before you are allowed to buy again (so the earliest re-buy is two days after a sale).

Return the maximum total profit you can achieve.

Examples

  • [1, 2, 3, 0, 2]3 — buy day 0 (1), sell day 1 (2), cooldown day 2, buy day 3 (0), sell day 4 (2): (2-1) + (2-0) = 3. (Selling on day 2 instead would force a cooldown on day 3 and block the second buy.)
  • [1]0 — a single day gives no chance to sell.
  • [6, 1, 3, 2, 4, 7]6 — just buy at the 1 (day 1) and sell at the 7 (day 5): 7 - 1 = 6; no split schedule beats holding through.

Constraints

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

With n up to 5000, an exponential search over every buy/sell schedule is infeasible. The expected solution is O(n) with a constant number of states per day.

Think about it first

Hint 1 On each day you are in exactly one situation: you currently *hold* a share, you *just sold* today (so tomorrow is a forced cooldown), or you are *free* to buy. Track the best profit reachable in each situation as you sweep the days.
Hint 2 Let the day be one axis and the situation (hold / sold-today / free) be the other. That is a 2-D table `dp[day][state]`. Each cell only depends on yesterday's cells — write the transition from day `i-1` to day `i` for all three states.
Hint 3 `hold[i] = max(hold[i-1], free[i-1] - prices[i])`, `sold[i] = hold[i-1] + prices[i]`, `free[i] = max(free[i-1], sold[i-1])`. The cooldown is encoded by the fact that you can only reach `hold` from `free`, and you only reach `free` from `sold` or `free`. The answer is `max(sold[n-1], free[n-1])`.

Write your solution, then hit Run tests to check it — or get a mock grade from the AI coach.

The coach remembers this session — revise your code and ask again, and it grades your progress. It gives hints, not the answer.
Report a bug