Buy and sell with cooldown
Standard · Write code · about 15 min
prices[i] is the price of a stock on day i. Make as many trades as you like, holding at most one share at a time. After you sell, you cannot buy on the next day.
Return the largest total profit.
Example 1
Input:
[[1,2,3,0,2]]
Output:
3
Buy 1, sell 2, rest, buy 0, sell 2.
Example 2
Input:
[[1]]
Output:
0
One day: no trade.
Constraints
- 1 ≤ prices.length ≤ 5000
- 0 ≤ prices[i] ≤ 1000
Learn the idea first: State machine DP: stock problems, a visual lesson.
More State machine DP: stock problems problems
- Fill the hold, sold and rest arrays Warm-up
- Best time to buy and sell once Warm-up
- Buy and sell with a transaction fee Standard
- At most k transactions Standard