Best Time to Buy and Sell Stock with Cooldown

problem [https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/ 309. Best Time to Buy and Sell Stock with Cooldown].

Problem description
You are given an array  where   is the price of a given stock on the   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: transactions = [buy, sell, cooldown, buy, sell]


 * Example 2
 * Input: prices = [1]
 * Output: 0


 * Constraints

Problem analysis
This problem is similar to the problem Best Time to Buy and Sell Stock II in that we can make unlimited number of transactions. However, in this problem, there is a cool-down day after each selling of stock before you can purchase another stock.

So we can use two arrays:
 * 1)  , to keep track of the maximum balance you can have at hand on each day.   means the maximum amount at hand on day i after you have made 0.5, 1.5, 2.5, … transactions. For each iteration, we want to make   as high as possible.
 * 2)  , to keep track of the maximum profit you can have at hand on each day.   means the maximum profit you can gain on day i regarding all possible combinations of purchases and sales.

We can iterate from day 0 to day n-1 through all the prices. On each day i we will update  and   as follows:
 * For :
 * For :
 * For :
 * : The balance on day i if you do not buy stock on this day.
 * : The balance on day i if you do buy stock on this day. Note that since there is a cool-down period, we can only consider the maximum profit made two days ago.
 * : The balance on day i if you do not sell stock on this day.
 * : The balance on day i if you do buy stock on this day.
 * : The balance on day i if you do not buy stock on this day.
 * : The balance on day i if you do buy stock on this day. Note that since there is a cool-down period, we can only consider the maximum profit made two days ago.
 * : The balance on day i if you do not sell stock on this day.
 * : The balance on day i if you do buy stock on this day.
 * : The balance on day i if you do not sell stock on this day.
 * : The balance on day i if you do buy stock on this day.

After iterating through all the days,  will be the result.

Working example

 * Input: 5, 1, 2, 3, 7, 2, 5, 1, 3
 * Return: 8
 * Buy on day 1 → sell on day 4 (profit: 6)
 * Buy on day 7 → sell on day 8 (profit: 2)

Complexity analysis

 * Time complexity
 * Since we iterate through each day once, and the computation amount is constant for each single day, the total time complexity is O(n), where n is the total number of days.


 * Space complexity
 * Since we created two arrays for  and , each with n elements, where n is the total number of days, the total space complexity is O(n).