You are given an array prices where prices[i] is the price of a given stock on the ith day.

Find the maximum profit you can achieve. You may complete at most two transactions.

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

&nbsp;
Example 1:

Input: prices = [3,3,5,0,0,3,1,4]
Output: 6
Explanation: Buy on day 4 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.
Then buy on day 7 (price = 1) and sell on day 8 (price = 4), profit = 4-1 = 3.

Example 2:

Input: prices = [1,2,3,4,5]
Output: 4
Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are engaging multiple transactions at the same time. You must sell before buying again.


Example 3:

Input: prices = [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.


&nbsp;
Constraints:


	1 &lt;= prices.length &lt;= 105
	0 &lt;= prices[i] &lt;= 105


class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        buy1, sell1, buy2, sell2 = -inf,0,-inf,0
        for price in prices:
            buy1 = max(buy1,-price)
            sell1 = max(sell1,price+buy1)
            
            buy2 = max(buy2,sell1-price)
            sell2 = max(sell2,price+buy2)
        return sell2
class Solution {
    public int maxProfit(int[] prices) {
        
       int n = prices.length;
        int maxSellProfit = 0;
        int min = prices[0];
        int[] maxSellArr = new int[n];
        int i = 1;
        
        while(i < n){
            if(prices[i] < min){
                min = prices[i];
            }
            maxSellArr[i] = Math.max(maxSellArr[i-1],prices[i] - min);  
            
            i++;
        }
        int[] maxBuyArr = new int[n];
        int j = n-2;
        int max = prices[n-1];
        while(j >= 0){
            if(prices[j] > max){
                max = prices[j];
            }
            maxBuyArr[j] = Math.max(maxBuyArr[j+1],max - prices[j]);
            
            j--;
        }
        int maxProfitTwoTrans = 0;
        for(int k = 0; k < n; k++){
            maxProfitTwoTrans = Math.max(maxProfitTwoTrans,maxBuyArr[k] + maxSellArr[k]);
        }
        return maxProfitTwoTrans;
    }
}
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int n=prices.size();
        vector<int> left(n);
        vector<int> right(n);
        int mini=INT_MAX;
        int ans1=0;
        for(int i=0;i<n;i++)
        {
            mini=min(mini,prices[i]);
            ans1=max(ans1,prices[i]-mini);
            left[i]=ans1;
        }
        int maxi=INT_MIN;
        int ans2=0;
        for(int i=n-1;i>=0;i--)
        {
            maxi=max(maxi,prices[i]);
            ans2=max(ans2,maxi-prices[i]);
            right[i]=ans2;
        }
        int ans3=0;
        for(int i=0;i<n;i++)
        {
            ans3=max(ans3,left[i]+right[i]);
        }
        return ans3;
    }
};
var maxProfit = function(prices) {
  if(prices.length == 0) return 0
  
  let dp = new Array(prices.length).fill(0);
  let min = prices[0];
  let max = 0;
  for (let i = 1; i < prices.length; i++) {
    min = Math.min(min, prices[i]);  // or Math.min(min, prices[i] - dp[i]) , FYI: dp[i] is 0
    max = Math.max(max, prices[i] - min);
    dp[i] = max;
  }
  
  // 1st run dp = [0,0,2,2,2,3,3,4];
  
  min = prices[0];
  max = 0;
  for (let i = 1; i < prices.length; i++) {
    min = Math.min(min, prices[i] - dp[i]); // substract dp[i]  = current price - what profit we made during 1st run.
    max = Math.max(max, prices[i] - min);
    dp[i] = max;
  }
  
  // 2nd run dp = [0,0,2,2,2,5,5,6];
  
  return dp.pop();
};
Best Time to Buy and Sell Stock III