You are given a list of integers prices where prices[i] is the price of a stock on day i, in chronological order.
You may complete any number of transactions, but you can hold at most one share at a time. You must sell the share you are holding before you buy again, and you cannot buy and sell on the same day. After you sell on day j, you cannot buy again until day j + 2 or later. Note that you are not required to trade at all.
Return the maximum total profit you can make.
Examples
Example 1
Input: prices = [1,2,3,0,2]
Output: 3
Explanation: Buy on day 0 (price 1) and sell on day 1 (price 2) for a profit of 1. Day 2 is a forced cooldown day, so the next buy is on day 3 (price 0), sold on day 4 (price 2) for a profit of 2. Total profit is 1 + 2 = 3.
Example 2
Input: prices = [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 1 (price 1) and sell on day 4 (price 6) for a profit of 5. Selling early on day 2 (price 5) for a profit of 4 would force a cooldown on day 3 and miss the higher price on day 4, so holding through day 3 is better.
Example 3
Input: prices = [1]
Output: 0
Explanation: There is no day left to sell on, and you are not required to trade, so the profit is 0.
Constraints
0≤n≤105 where n=prices.length
0≤prices[i]≤104
Loading editor...
Run checks the sample cases; Submit runs every case.
Samples 3
Custom 0
passedwrong answertime limiterrorran, no expected valuenot run