Best Time to Buy and Sell Stock with Cooldown

Given stock prices, return the maximum profit you can achieve with as many transactions as you like, but after selling a stock you must wait one day before buying again. This is a standard recurrence-based DP with multiple states.

Input Format

prices = stock prices by day

Output Format

maximum profit with 1-day cooldown after selling

Constraints

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

Examples

Example 1:

Input:

prices = [1,2,3,0,2]

Output:

3

Explanation:

Buy on day 1, sell on day 3, cooldown on day 4, buy on day 4, sell on day 5.

Example 2:

Input:

prices = [1]

Output:

0

Explanation:

There is no opportunity to sell.

Loading...
Best Time to Buy and Sell Stock with Cooldown