<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>

import functools
from itertools import accumulate

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        # IDEA 0: Brute force would be trying essentially all (i,j,k,l): O(n^4)

        # IDEA 1: We can use our dp solution for 1 transaction to get O(n^3)
        """
        # Max profit selling at i using <= 1 transaction
        @functools.cache
        def dp_1(i):
            if i <= 0:
                return 0
            return max(0, dp_1(i-1) - prices[i-1] + prices[i])
        
        scan = [0]
        for i in range(1, len(prices)):
            scan.append(max(scan[i-1], dp_1(i)))

        max_profit = 0
        for i in range(len(prices)):
            for j in range(i+1, len(prices)):
                # profit from second transaction at (i, j)
                # print(f'i: {i}, j: {j}, dp_1(<= i): {scan[i]}')
                second_profit = prices[j] - prices[i]
                max_profit = max(max_profit, second_profit + scan[i])
        return max_profit
        """

        # IDEA 2: Define dp_1(<= i) and dp_2(>= i). Then, iterate over all
        # split-points p in range(len(prices)) and take dp_1(p) + dp_2(p)

        # Max among prices[<= i] selling at i
        @functools.cache
        def dp_le(i):
            if i <= 0:
                return 0
            return max(0, dp_le(i-1) - prices[i-1] + prices[i])
        
        # Max among prices[>= i] buying at i
        @functools.cache
        def dp_ge(i):
            if i >= len(prices) - 1:
                return 0
            return max(0, dp_ge(i+1) + prices[i+1] - prices[i])

        all_dp_le = list(map(lambda tup : dp_le(tup[0]), enumerate(prices)))
        dp_le_maxes = list(accumulate(all_dp_le, lambda x, y : max(x,y)))
        all_dp_ge = list(map(lambda tup : dp_ge(tup[0]), enumerate(prices)))
        all_dp_ge.reverse()
        dp_ge_maxes = list(accumulate(all_dp_ge, lambda x, y : max(x,y)))
        dp_ge_maxes.reverse()
        # print(dp_le_maxes,'dp_le_maxes')
        # print(dp_ge_maxes,'dp_ge_maxes')
        max_profit = 0
        for split in range(len(prices)):
            max_profit = max(max_profit, dp_le_maxes[split] + dp_ge_maxes[split])
        return max_profit

220226