Skip to content
Onsite

Best Time to Buy and Sell Stock with Cooldown

MediumP3Coding

prices[i] is a stock price on day i. You may buy and sell as many times as you like, but you hold at most one share at a time, and after you sell you must skip the next day (a one-day cooldown) before you can buy again.

Return the maximum total profit.

Example 1

Input
prices = [1,2,3,0,2]
Output
3
Why
Buy at 1, sell at 2, cool down, buy at 0, sell at 2: profit 1 + 2 = 3.

Example 2

Input
prices = [1]
Output
0

Example 3

Input
prices = [2,1]
Output
0

Constraints

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

Hints

Run checks the 3 visible tests, Submit all 11.