<p>You are given an integer 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>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy it then immediately sell it on the <strong>same day</strong>.</p>

<p>Find and return <em>the <strong>maximum</strong> profit you can achieve</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> prices = [7,1,5,3,6,4]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.
Total profit is 4 + 3 = 7.
</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.
Total profit is 4.
</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> There is no way to make a positive profit, so we never buy the stock to achieve the maximum profit of 0.
</pre>

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

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

class Solution {
public:

    typedef struct State {
        int price;
        int profit;

        State(int v) {
            price = v;
            profit = v;
        }
    } State;

    int maxProfit(vector<int>& prices) {

        vector<State> hold_some(prices.size()+1, 0);
        vector<State> hold_none(prices.size()+1, 0);
        vector<State> buy(prices.size()+1, 0);
        vector<State> sell(prices.size()+1, 0);

        buy[0].price = prices[0];
        hold_some[0].price = prices[0];
        
        int profit = 0;
        for(int i = 1; i < prices.size(); ++i ) {

            profit = std::max(profit, buy[i].profit);

            //sell
            int use_last_buy = buy[i-1].profit + (prices[i] - buy[i-1].price);
            int use_last_hold = hold_some[i-1].profit + (prices[i] - hold_some[i-1].price);
            if(use_last_buy > use_last_hold)     {
                printf("sell: at %d buy %d \n", prices[i], buy[i-1].price);
                sell[i].profit = use_last_buy;
            } else {
                printf("sell: at %d hold price:%d  profit:%d\n", prices[i], hold_some[i-1].price, hold_some[i-1].profit);
                sell[i].profit = use_last_hold;
            }

            //buy
            if(sell[i-1].profit > hold_none[i-1].profit) {
                buy[i].profit = sell[i-1].profit;
                printf("buy: at %d from sell %d \n", prices[i], buy[i].profit);
            } else {
                buy[i].profit = hold_none[i-1].profit;
                printf("buy: at %d from hold_none %d \n", prices[i], buy[i].profit);
            }
            if(sell[i].profit > buy[i].profit) {
               buy[i].profit = sell[i].profit; 
            }

            buy[i].price = prices[i];  

            profit = std::max(profit, sell[i].profit);

            //hold_some
            if(buy[i-1].profit > hold_some[i-1].profit) {
                hold_some[i].profit = buy[i-1].profit;
                hold_some[i].price = buy[i-1].price;
            } else {
                hold_some[i].profit = hold_some[i-1].profit;
                hold_some[i].price = hold_some[i-1].price;
            }
            profit = std::max(profit, hold_some[i].profit);

            //hold_none
            if(sell[i-1].profit > hold_none[i-1].profit) {
                hold_none[i].profit = sell[i-1].profit;
            } else {
                hold_none[i].profit = hold_none[i-1].profit;
            }
            profit = std::max(profit, hold_none[i].profit);
        }

        return profit;
    }
};
17400