Best Time to Buy and Sell Stock with Cooldown

MediumArray

Description

Given an array prices, find the maximum profit. You may complete as many transactions as you like but must wait one day after selling before buying again (cooldown).

Examples

Input:prices = [1,2,3,0,2]
Output:3
Explanation:

Buy at 1, sell at 3, cooldown, buy at 0, sell at 2. Profit = 3.

Input:prices = [1]
Output:0
Explanation:

With only one day of prices, you cannot complete a buy-sell transaction, so profit is 0.

Input:prices = [7,1,5,3,6,4]
Output:5
Explanation:

Buy at 1, sell at 5 (profit = 4), cooldown at 3, buy at 3, sell at 6 (profit = 3). Total profit = 4 + 3 = 7.

Constraints

  • 1 ≤ prices.length ≤ 5000
  • 0 ≤ prices[i] ≤ 1000

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!