<p>You are given an integer 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, and an integer <code>k</code>.</p>

<p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k</code> times and sell at most <code>k</code> times.</p>

<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> k = 2, prices = [2,4,1]
<strong>Output:</strong> 2
<strong>Explanation:</strong> Buy on day 1 (price = 2) and sell on day 2 (price = 4), profit = 4-2 = 2.
</pre>

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

<pre>
<strong>Input:</strong> k = 2, prices = [3,2,6,5,0,3]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Buy on day 2 (price = 2) and sell on day 3 (price = 6), profit = 6-2 = 4. Then buy on day 5 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.
</pre>

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

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

class Solution {
public:
/*
    int MaxProfitRecursion(vector<int> &prices,int index,int buy,int count){
        if(index>=prices.size()){
            return 0;
        }
        if(count==0){
            return 0;
        }
        int Profit=0;
        if(buy==1){            
            int IncludeStock=-prices[index]+MaxProfitRecursion(prices,index+1,0,count);
            int ExcludeStock=0+MaxProfitRecursion(prices,index+1,1,count);
            Profit=max(IncludeStock,ExcludeStock);            
        }
        else{            
            int SellStock=prices[index]+MaxProfitRecursion(prices,index+1,1,count-1);
            int KeepStock=0+MaxProfitRecursion(prices,index+1,0,count);
            Profit=max(SellStock,KeepStock);            
        }
        return Profit;
    }
*/
    int MaxProfitMemoization(vector<int> &prices,int index,int buy,int count,vector<vector<vector<int>>> &dp){
        if(index>=prices.size()){
            return 0;
        }
        if(count==0){
            return 0;
        }
        if(dp[index][buy][count]!=-1){
            return dp[index][buy][count];
        }
        int Profit=0;
        if(buy==1){            
            int IncludeStock=-prices[index]+MaxProfitMemoization(prices,index+1,0,count,dp);
            int ExcludeStock=0+MaxProfitMemoization(prices,index+1,1,count,dp);
            Profit=max(IncludeStock,ExcludeStock);            
        }
        else{            
            int SellStock=prices[index]+MaxProfitMemoization(prices,index+1,1,count-1,dp);
            int KeepStock=0+MaxProfitMemoization(prices,index+1,0,count,dp);
            Profit=max(SellStock,KeepStock);            
        }
        dp[index][buy][count]=Profit;
        return dp[index][buy][count];
    }
    int MaxProfitTabulation(vector<int>& prices,int k){
        int N=prices.size();
        vector<vector<vector<int>>> dp(N+1,vector<vector<int>> (2,vector<int> (k+1,0)));

        for(int index=N-1;index>=0;index--){
            for(int buy=0;buy<=1;buy++){
                for(int count=0;count<=k;count++){
                    int Profit=0;
                    if(buy==1){            
                        int IncludeStock=-prices[index]+dp[index+1][0][count];
                        int ExcludeStock=0+dp[index+1][1][count];
                        Profit=max(IncludeStock,ExcludeStock);            
                    }
                    else{      
                        int SellStock=0;
                        if(count>0){
                            SellStock=prices[index]+dp[index+1][1][count-1];
                        }      
                        int KeepStock=0+dp[index+1][0][count];
                        Profit=max(SellStock,KeepStock);            
                    }
                    dp[index][buy][count]=Profit;
                }
            }
        }
        return dp[0][1][k];
    }
    int maxProfit(int k, vector<int>& prices) {
        int index=0;
        int count=k;
        int buy=1;
        int N=prices.size();
        vector<vector<vector<int>>> dp(N+1,vector<vector<int>> (2,vector<int> (count+1,-1)));
        int answer=MaxProfitTabulation(prices,count);
        return answer;
    }
};
20026