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

<p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy it then immediately sell it on the <strong>same day</strong>.</p>

<p>Find and return <em>the <strong>maximum</strong> profit you can achieve</em>.</p>

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

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

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

<pre>
<strong>Input:</strong> prices = [1,2,3,4,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
Total profit is 4.
</pre>

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

<pre>
<strong>Input:</strong> prices = [7,6,4,3,1]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There is no way to make a positive profit, so we never buy the stock to achieve the maximum profit of 0.
</pre>

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

<ul>
	<li><code>1 &lt;= prices.length &lt;= 3 * 10<sup>4</sup></code></li>
	<li><code>0 &lt;= prices[i] &lt;= 10<sup>4</sup></code></li>
</ul>

class Solution {
private:
    int recursion(int ind, int buy, vector<int>&prices){
        // base case
        if(ind==prices.size()) return 0;
        // logic
        int profit=0;
        if(buy){
            profit = max(-prices[ind] + recursion(ind+1, 0, prices), 
                            recursion(ind+1, 1, prices));
        }else{
            profit = max(prices[ind] + recursion(ind+1, 1, prices),
                            recursion(ind+1, 0, prices));
        }
        return profit;
    }

    int memoization(int ind, int buy, vector<int>&prices, vector<vector<int>>&dp){
        // base case
        if(ind==prices.size()) return 0;
        if(dp[ind][buy]!=-1) return dp[ind][buy];
        // logic
        int profit=0;
        if(buy){
            profit = max(-prices[ind] + memoization(ind+1, 0, prices, dp), 
                            memoization(ind+1, 1, prices, dp));
        }else{
            profit = max(prices[ind] + memoization(ind+1, 1, prices, dp),
                            memoization(ind+1, 0, prices, dp));
        }
        return dp[ind][buy] = profit;
    }

    int tabulation(int n, vector<int>& prices,  vector<vector<int>>& dp){
        for(int i=n-1; i>=0; i--){
            for(int j=0;j<2;j++){
                long profit=0;
                if(j){
                    profit = max(-prices[i] + dp[i+1][0], 
                                    dp[i+1][1]);
                }else{
                    profit = max(prices[i] + dp[i+1][1],
                                    dp[i+1][0]);
                }
                dp[i][j] = profit;
            }
        }
        return dp[0][1];
    }

    // int tabulation(int n, vector<int>& prices){
    //     vector<int> last(2);
    //     for(int i=n-1; i>=0; i--){
    //         vector<int> curr(2);
    //         for(int j=0;j<2;j++){
    //             long profit=0;
    //             if(j){
    //                 profit = max(-prices[i] + last[0], 
    //                                 last[1]);
    //             }else{
    //                 profit = max(prices[i] + last[1],
    //                                 last[0]);
    //             }
    //             curr[j] = profit;
    //         }
    //         last = curr;
    //     }
    //     return last[1];
    // }

    int tabulation(int n, vector<int>& prices){
        vector<int> last(2);
        for(int i=n-1; i>=0; i--){
            vector<int> curr(2);
                int profit=0;
                curr[1] = max(-prices[i] + last[0], 
                                last[1]);
                curr[0] = max(prices[i] + last[1],
                                last[0]);
            last = curr;
        }
        return last[1];
    }

public:
    int maxProfit(vector<int>& prices) {
        // return recursion(0, 1, prices);
        int n = prices.size();
        // vector<vector<int>> dp(n, vector<int>(2, -1));
        // return memoization(0, 1, prices, dp);
        // vector<vector<int>> Tdp(n+1, vector<int>(2, 0));
        // return tabulation(n, prices, Tdp);
        return tabulation(n, prices);
    }
};
17100