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

    enum States
    {
        BUY = 0,
        SELL,
        COOL_DOWN
    };

    struct data 
    {
        States state;
        int index;

        bool operator<(const data &d) const
        {
            return (index == d.index)? (state < d.state) : (index < d.index);
        }
    };

    map<data, int> dp_Memo;


    int maxProfitHelper(vector<int>& prices, States state, int index)
    {
        if(index>=prices.size())
            return 0;
        data d;
        d.state = state;
        d.index = index;
        if(dp_Memo.find(d)!=dp_Memo.end())
            return dp_Memo[d];
            
        if(state == BUY)  //buy
        {
            int result =  max(
                maxProfitHelper(prices, BUY, index + 1),                  //goto next without buying now
                maxProfitHelper(prices, SELL, index + 1) - prices[index]   //buy now and go to next in sell state
            );
            dp_Memo[d] = result;
            return result;
        }
        else if(state == SELL) //sell
        {
             int result = max(
                maxProfitHelper(prices, SELL, index + 1),                       //goto next and sell at later date
                maxProfitHelper(prices, COOL_DOWN, index + 1) + prices[index]   //sell now, go to cool down, and return profit
            );
            dp_Memo[d] = result;
            return result;
        }
        else //if(state == COOL_DOWN)    //cooldown
        {
            int result = maxProfitHelper(prices, BUY, index + 1);   //goto next without buying
            dp_Memo[d] = result;
            return result;
        }
    }

    int maxProfit(vector<int>& prices) {

        return maxProfitHelper(prices, BUY, 0);

        /*        
        if(prices.size()==0)
            return 0;

        vector<int> maxProfit(prices.size(), 0);
        for(int i=0;i<prices.size(); i++)
        {
            int maxProfitBefore = 0;
            int currProfit = 0;
            if(i>=1)
            {
                maxProfitBefore = maxProfit[i-1];
                currProfit = prices[i] -  prices[i-1];

                //there is loss
                if(currProfit<0)
                {
                    //currProfit = 0;
                    cout<<"S ";
                }
                else
                    cout<<"K ";

            }
            maxProfit[i] = maxProfitBefore + currProfit;
            cout<<prices[i]<<" Before "<<maxProfitBefore<<" CurProfit "<<currProfit<<" Max "<< maxProfit[i] <<endl;
        }
        return maxProfit[prices.size()-1];
        */        

        /*
        int maxAmount = 0;
        
        int maxProfitCurr = 0;
        int minAmount = prices[0];
        int minPos = 0;
       

        //save all the values is the list
        //look at the previous values and decide maximum

        for(int i=1;i<prices.size(); i++)
        {
            int &currAmount = prices[i];   
            cout<<currAmount<<" M "<<maxAmount<<" CP "<<maxProfitCurr<<" CM "<<minAmount<<" CMI "<<minPos<<endl;
            if(minAmount>currAmount)
            {
                minAmount = currAmount;
                minPos = i;
            }

            int &lastAmount = prices[i-1];
            if(currAmount<lastAmount)
            {
                maxAmount += maxProfitCurr;
                maxProfitCurr = 0;
                minAmount = currAmount;
                minPos = i;
                cout<<"Sell "<<maxAmount<<endl;
            }
            else
            {
                int currProfit = currAmount - minAmount;
                if(maxProfitCurr<currProfit)
                {
                    maxProfitCurr = currProfit;                
                }
            }
            cout<<currAmount<<" M "<<maxAmount<<" CP "<<maxProfitCurr<<" CM "<<minAmount<<" CMI "<<minPos<<endl;
        }
        maxAmount += maxProfitCurr;

        return maxAmount;   
        */     
    }
};
17500