天天看點

[LeetCode] 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 (ie, you must sell the stock before you buy again).

建立兩個數組<code>left</code>和<code>right</code>,分别存儲某個元素左邊和右邊所能獲得的最大收益。即<code>left[i]</code>存儲從<code>[0, i]</code>範圍的最大收益;<code>right[i]</code>存儲從<code>[i, len - 1]</code>範圍的最大收益。