You are given an array prices where prices[i] is the price of a given stock on the i-th day. Find the maximum profit you can achieve. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the following restrictions: - After you sell your stock, you cannot buy stock on the next day (i.e., cooldown one day). Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
Example 1
Input: prices = [1,2,3,0,2]
Output: 3
Explanation: The best plan is buy, sell, cooldown, buy, sell: buy at 1, sell at 2 (+1), rest on the day priced 3, buy at 0 and sell at 2 (+2). The total is 1 + 2 = 3.
Example 2
Input: prices = [1]
Output: 0
Explanation: With only one day there is no chance to buy and then sell, so the profit is 0.
Example 3
Input: prices = [1,2,4]
Output: 3
Explanation: Buy at 1 and sell at 4 for a profit of 3. No cooldown is needed because only one trade is made.
Each day you are in one of three states: holding a share, just sold (must cool down), or resting (free to buy). Track the best profit for each state and roll them forward.
Run checks all cases above. Submit evaluates all test cases.