Skip to content
Decoder

Best Time to Buy and Sell Stock with Cooldown

Medium

Problem

prices[i] is a stock's price on day i. You may make as many transactions as you like (buy one share, then sell it), with two rules: you can hold at most one share at a time, and after you sell you must wait one day (a one-day cooldown) before buying again. Return the maximum profit.

Example 1

Input
prices = [1, 2, 3, 0, 2]
Output
3
Why
buy 1, sell 2, cooldown, buy 0, sell 2

Example 2

Input
prices = [1]
Output
0

Constraints

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

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.