<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 {
public:
    int solve(int index,vector<int>& prices, int buy){
        if(index==prices.size()){
            return 0;
        }
        int profit = 0;
        if(buy){
            profit = max((-prices[index])+solve(index+1,prices,0),(0+solve(index+1,prices,1)));
        }
        else{
            profit = max((prices[index])+solve(index+1,prices,1),(0+solve(index+1,prices,0)));
        }
        return profit;
    }

    int solveMem(int index,vector<int>& prices, int buy,vector<vector<int>> &dp){
        if(index==prices.size()){
            return 0;
        }
        if(dp[index][buy]!=-1){
            return dp[index][buy]; 
        }
        int profit = 0;
        if(buy){
            profit = max((-prices[index])+solveMem(index+1,prices,0,dp),(0+solveMem(index+1,prices,1,dp)));
        }
        else{
            profit = max((prices[index])+solveMem(index+1,prices,1,dp),(0+solveMem(index+1,prices,0,dp)));
        }
        return dp[index][buy] = profit;
    }

    int solveTab(vector<int>& prices){
        int n = prices.size();
        vector<vector<int>> dp(n+1,vector<int>(2,0));
        for(int index = n-1;index>=0;index--){
            for(int buy = 0 ; buy<=1;buy++){
                int profit = 0;
                if(buy){
                    profit = max((-prices[index])+dp[index+1][0],(0+dp[index+1][1]));
                }
                else{
                    profit = max((prices[index])+dp[index+1][1],(0+dp[index+1][0]));
                }
                dp[index][buy] = profit;
            }
        }
        return dp[0][1];
    }

    int solveSpaceOpti(vector<int>& prices){
        int n = prices.size();
        vector<int> curr(n+1,0);
        vector<int> next(n+1,0);

        for(int index = n-1;index>=0;index--){
            for(int buy = 0 ; buy<=1;buy++){
                int profit = 0;
                if(buy){
                    int buyKaro = (-prices[index])+next[0];
                    int skipKaro = 0+next[1];
                    profit = max(buyKaro,skipKaro);
                }
                else{
                    int sellKaro = prices[index]+next[1];
                    int skipKaro = 0+next[0];
                    profit = max(sellKaro,skipKaro);
                }
                curr[buy] = profit;
            }
            next = curr;
        }
        return next[1];
    }
    int maxProfit(vector<int>& prices) {
        int n = prices.size();
        //return solve(0,prices,1);
        //vector<vector<int>> dp(n+1,vector<int>(2,-1));
        return solveSpaceOpti(prices);

    }
};
16300