<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 {
public:
    std::vector<int> memo;
    std::vector<int> min0;
    std::vector<int> mins;
    int num;


    int readMemo(int pos, int trans) const {
        return memo[pos+num*(trans)];
    }

    void writeMemo(int pos, int trans, int val) {
        memo[pos+num*(trans)] = val;
    }

    int mp(int pos, int trans, const vector<int>& prices) {
        if (trans < 0) return 0;
        if (pos == 0) {
            min0[pos] = prices[pos];
            mins[pos] = prices[pos];
            return 0;
        }
        int m = readMemo(pos, trans);
        if (m >= 0) return m;
        if (trans > 0) {
            int v1 = mp(pos-1, trans, prices);      // profit of doing nothing
            int v2 = mp(pos-1, trans-1, prices);    // max profit of the previous trade to this point
            int profit = max(v1, prices[pos]-mins[pos-1]);
            writeMemo(pos, trans, profit);
            return profit;
        } else {
            // first transaction special case
            int prev = mp(pos-1, 0, prices);            // this builds the whole table on first call
            int diff = prices[pos] - min0[pos-1];
            int profit = std::max(prev, diff);
            min0[pos] = (prices[pos] < min0[pos-1]) ? prices[pos] : min0[pos-1];
            mins[pos] = std::min(mins[pos-1], prices[pos]-prev);
            writeMemo(pos, trans, profit);
            return profit;
        }
    }

    int maxProfit(vector<int>& prices) {
        memo.resize(prices.size() * 4,-1);
        min0.resize(prices.size() * 1,-1);
        mins.resize(prices.size(), -1);
        num = prices.size();
        int res =  mp(prices.size()-1, 1, prices);
#if 0
        printf("min0: ");
        for (int i=0; i< prices.size(); i++) printf("%d ", min0[i]);
        printf("\n");
        printf("mins: ");
        for (int i=0; i< prices.size(); i++) printf("%d ", mins[i]);
        printf("\n");
        for (int i=0; i<2; i++) {
            printf("t%d: ", i);
            for (int j=0; j<prices.size(); j++) printf("%d ", memo[i*num+j]);
            printf("\n");
        }
#endif
        return res;
    }
};

103413