<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p>

<p>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:</p>

<ul>
	<li>After you sell your stock, you cannot buy stock on the next day (i.e., cooldown one day).</li>
</ul>

<p><strong>Note:</strong> You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> prices = [1,2,3,0,2]
<strong>Output:</strong> 3
<strong>Explanation:</strong> transactions = [buy, sell, cooldown, buy, sell]
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> prices = [1]
<strong>Output:</strong> 0
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= prices.length &lt;= 5000</code></li>
	<li><code>0 &lt;= prices[i] &lt;= 1000</code></li>
</ul>

class Solution {
public:
   vector<vector<int>> dp;
    //cooldown 1 means cooldown laga hai
    //0 means u r free to buy
    int helper(int index, int canBuy, vector<int>& prices) {
        if (index >= prices.size()) {
            return 0;
        }

        if (dp[index][canBuy]!= -1) {
            return dp[index][canBuy];
        }

        int buy = INT_MIN, sell = INT_MIN;
        if (canBuy) {
            buy = -prices[index] + helper(index + 1,0,prices);
        } else if(!canBuy){
            sell = prices[index] + helper(index + 2,1,prices);
        }
        //cooldown will reset to zero if we do nothing
        int doNothing = helper(index + 1, canBuy,prices);

        return dp[index][canBuy]= max({buy, sell, doNothing});
    }

//   int tabu(vector<int>& prices) {
//         int n = prices.size();

//         // base case last index has 0
//         dp.assign(n+1, vector<vector<int>>(2, vector<int>(2, 0)));

//         for (int index = n - 1; index >= 0; index--) {
//             for (int canBuy = 1; canBuy >= 0; canBuy--) {
//                 for (int cooldown = 1; cooldown >= 0; cooldown--){
//                     int buy = INT_MIN, sell = INT_MIN;
//                 if (canBuy && !cooldown) {
//                     buy = -prices[index] + dp[index + 1][0][cooldown];
//                 } else if (!canBuy) {
//                     sell = prices[index] + dp[index + 1][1][1];
//                 }
//                 int doNothing = dp[index + 1][canBuy][0];

//                 dp[index][canBuy][cooldown] = max({buy, sell, doNothing});
//             }
//             }
//         }
//         return dp[0][1][0];
//     }

     int space(vector<int>& prices) {
        int n = prices.size();

        // base case last index has 0
        vector<vector<int>>next(2, vector<int>(2, 0));
        vector<vector<int>>curr(2, vector<int>(2, 0));

        for (int index = n - 1; index >= 0; index--) {
            for (int canBuy = 1; canBuy >= 0; canBuy--) {
                for (int cooldown = 1; cooldown >= 0; cooldown--){
                    int buy = INT_MIN, sell = INT_MIN;
                if (canBuy && !cooldown) {
                    buy = -prices[index] + next[0][cooldown];
                } else if (!canBuy) {
                    sell = prices[index] + next[1][1];
                }
                int doNothing = next[canBuy][0];

                curr[canBuy][cooldown] = max({buy, sell, doNothing});
            }
            }
            next=curr;
        }
        return next[1][0];
    }
    
    int maxProfit(vector<int>& prices) {
         int n=prices.size();
        dp.assign(n,vector<int>(2,-1));
        return helper(0,1,prices);

        // return tabu(prices);

        // return space(prices);
    }
};
16200