# 123. Best Time to Buy and Sell Stock III

Say you have an array for which the ith element is the price of a given stock on day i.

Design an algorithm to find the maximum profit. You may complete at most two transactions.

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

Example 1:

Input: [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: [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: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.

# Solution

Approach 1: DP. Make a transaction once by finding the best deal so far from the left and right, then find a split point where it maximizes left[i] + right[i+1].

Approach 2: DP. Separate the first and second transactions.

# Code (Python)

Approach 1:

    def maxProfit(self, prices: List[int]) -> int:
        if len(prices) <= 1:
            return 0
        # make a transaction once, find the best deal so far from the left and right
        # then find a split point where it maximizes left[i] + right[i+1]
        left = [0]
        lowest = prices[0]
        for i in range(1, len(prices)):
            left.append(max(left[-1], prices[i] - lowest))
            lowest = min(lowest, prices[i])
        right = [0] * len(prices)
        highest = prices[-1]
        for i in range(len(prices) - 2, -1, -1):
            right[i] = max(right[i+1], highest - prices[i])
            highest = max(highest, prices[i])

        max_profit = left[-1]
        for i in range(len(prices) - 1):
            max_profit = max(max_profit, left[i] + right[i+1])
        
        return max_profit

# Code (C++)

Approach 1:

// Make a transaction once, find the best deal so far from the left and right
// then find a split point where it maximizes left[i] + right[i+1].
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int n = prices.size();
        if (n < 2)
            return 0;
        // From left to right.
        int leftBuy = 0 - prices[0];
        int leftSell[n];
        leftSell[0] = 0;
        for (int i = 1; i < n; ++i)
        {
            leftSell[i] = std::max(leftSell[i-1], leftBuy + prices[i]);
            leftBuy = std::max(leftBuy, 0 - prices[i]);
        }
        // From right to left.
        int rightBuy = 0 - prices[n-1];
        int rightSell[n];
        rightSell[n-1] = 0;
        for (int i = n-2; i >= 0; --i)
        {
            rightSell[i] = std::min(rightSell[i+1], rightBuy + prices[i]);
            rightBuy = std::min(rightBuy, 0 - prices[i]);
        }
        // Get the max split point.
        int maxVal = 0;
        for (int i = 0; i < n; ++i)
        {
            maxVal = std::max(maxVal, leftSell[i] - rightSell[i]);
        }
        return maxVal;
    }
};

Approach 2:

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int n = prices.size();
        if (n < 2)
            return 0;
        vector<int> buy1(n+1, 0);
        vector<int> buy2(n+1, 0);
        vector<int> sell1(n+1, 0);
        vector<int> sell2(n+1, 0);
        buy1[0] = INT_MIN;
        buy2[2] = INT_MIN;
        for (int i = 1; i <= n; ++i)
        {
            buy1[i] = std::max(buy1[i-1], 0 - prices[i-1]);
            if (i > 1)
                sell1[i] = std::max(sell1[i-1], buy1[i-1] + prices[i-1]);
            if (i > 2)
                buy2[i] = std::max(buy2[i-1], sell1[i-1] - prices[i-1]);
            if (i > 3)
                sell2[i] = std::max(sell2[i-1], buy2[i-1] + prices[i-1]);
        }
        return std::max(sell1[n], sell2[n]);
    }
};
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int n = prices.size();
        if (n < 2)
            return 0;
        int prevB1 = 0 - prices[0];
        int prevS1 = 0;
        int prevB2 = 0 - prices[1];
        int prevS2 = 0;
        for (int i = 1; i < n; ++i)
        {
            int currB1 = std::max(prevB1, 0 - prices[i]);
            int currS1 = std::max(prevS1, prevB1 + prices[i]);
            int currB2 = 0;
            if (i > 1)
                currB2 = std::max(prevB2, prevS1 - prices[i]);
            int currS2 = 0;
            if (i > 2)
                currS2 = std::max(prevS2, prevB2 + prices[i]);
            prevB1 = currB1;
            prevS1 = currS1;
            if (i > 1)
                prevB2 = currB2;
            if (i > 2)
                prevS2 = currS2;
        }
        return std::max(prevS1, prevS2);
    }
};
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int n = prices.size();
        if (n < 2)
            return 0;
        int res = 0;
        vector<int> buy(2, INT_MIN);
        vector<int> sell(2, 0);
        for (int i = 0; i < n; ++i)
        {
            sell[0] = std::max(sell[0], buy[0] + prices[i]);
            buy[0] = std::max(buy[0], 0 - prices[i]);
            sell[1] = std::max(sell[1], buy[1] + prices[i]);
            buy[1] = std::max(buy[1], sell[0] - prices[i]);
        }
        return std::max(sell[0], sell[1]);
    }
};