<p>You are given an 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>Find the maximum profit you can achieve. You may complete <strong>at most two transactions</strong>.</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> prices = [3,3,5,0,0,3,1,4]
<strong>Output:</strong> 6
<strong>Explanation:</strong> 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.</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.
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.
</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> In this case, no transaction is done, i.e. max profit = 0.
</pre>

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

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

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        n = len(prices)
        memo = [[[-1] * 3 for _ in range(2)] for _ in range(n)]

        # for to_buy in range(2):
        #     for counter in range(3):
        #         dp[n][to_buy][counter] = 0

        # for idx in range(n):
        #     for to_buy in range(2):
        #         for counter in range(3):
        #             profit = None
        #             if to_buy:
        #                 dont_buy = 0 + dp[idx+1][True][counter]
        #                 buy = 0
        #                 if counter < 2:
        #                     buy = (-1 * prices[idx]) + dp[idx+1][False][counter]
        #                 profit = max(buy, dont_buy)

        #             else:
        #                 dont_sell = 0 + dp[idx+1][False][counter]
        #                 sell = prices[idx] + dp[idx+1][True][counter]
        #                 profit = max(sell, dont_sell)

        #             dp[idx][int(to_buy)][counter] = profit
                    
        # return dp[]
        def recurse(idx, to_buy, counter):
            nonlocal n, memo
            if idx == n:
                return 0
            if counter == 0:
                return 0

            # print(idx, to_buy, counter)
            if memo[idx][to_buy][counter] != -1:
                return memo[idx][int(to_buy)][counter]

            profit = None
            if to_buy:
                dont_buy = 0 + recurse(idx+1, True, counter)
                buy = (-1 * prices[idx]) + recurse(idx+1, False, counter)
                profit = max(buy, dont_buy)

            else:
                dont_sell = 0 + recurse(idx+1, False, counter)
                sell = prices[idx] + recurse(idx+1, True, counter - 1)
                profit = max(sell, dont_sell)

            memo[idx][int(to_buy)][counter] = profit
            return profit

        return recurse(0, True, 2)
81678