#309

Best Time to Buy and Sell Stock with Cooldown

specialist · 680 · lc medium +30 · verified · 61.8% accepted · 10,027 likes · top 62%

play →

Description

You are given an array prices where prices[i] is the price of a given stock on day i.

You may complete as many buy-sell transactions as you wish, subject to:

- You must sell before buying again.

- After selling, you cannot buy on the next day (one-day cooldown).

Return the maximum profit you can achieve.

Example 1:

Input: prices = [1,2,3,0,2]
Output: 3
Explanation: transactions = [buy, sell, cooldown, buy, sell]

Example 2:

Input: prices = [1]
Output: 0

Code

1
2
3