Best Time to Buy and Sell Stock II

problem 122: Best Time to Buy and Sell Stock II.

Problem description
You are given an array  where   is the price of a given stock on the   day.

Find the maximum profit you can achieve. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).

Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).


 * Example 1
 * Input: prices = [7,1,5,3,6,4]
 * Output: 7
 * Explanation: 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.


 * Example 2
 * Input: prices = [1,2,3,4,5]
 * Output: 4
 * Explanation: 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.


 * Example 3
 * Input: prices = [7,6,4,3,1]
 * Output: 0
 * Explanation: In this case, no transaction is done, i.e., max profit = 0.


 * Constraints

Problem analysis
Similar to the Best Time to Buy and Sell Stock problem, we use a  variable to keep track of the balance at hand after completing a half-transaction (i.e. only buying, no selling), and a   variable to keep track of the maximum profit we can gain. But one major difference is that we use  to keep track of the maximum balance possible after completing 0.5, 1.5, 2.5, … transactions; but in this problem the actual number of transactions is irrelevant, so we don't keep track of it.

To start with, we initialize  to 0, the profit we can get under the worst case; we initialize   to , since we want to keep increasing the balance as we iterate from left to right.

For each day we can choose one action among the three:
 * Do nothing
 * Purchase a new stock (not applicable on the last day)
 * Sell a stock (not applicable on the first day; only available when we have it at hand)

Just like in the Best Time to Buy and Sell Stock problem, we iterate from left to right and, in each iteration, update the  and   variables as follows:
 * 1) For  :
 * 2) * If we do not sell on day i, then our profit is still  from the previous day.
 * 3) * If we do sell on day i, then our profit would be . Pick whichever option yielding greater profit.
 * 4) For  :
 * 5) * If we do not buy on day i, then our balance is still  from the previous day.
 * 6) * If we do buy on day i, then our balance would be . Pick whichever option yielding greater balance at our hand.

After looping through all the prices, we return  as the result.

Working example

 * Input: 5, 1, 2, 3, 7, 2, 5, 1, 3
 * Return: 11
 * Buy on day 1 → sell on day 4 (profit: 6; total profit: 6)
 * Buy on day 5 → sell on day 6 (profit: 3; total profit: 9)
 * Buy on day 7 → sell on day 8 (profit: 2; total profit: 11)

Complexity analysis

 * Time complexity
 * We do the problem one-pass. When iterating on the price of each day, we update the  and   exactly once during each iteration, with O(1) time complexity for each of them. So the total time complexity is O(n), where n is the number of days.


 * Space complexity
 * Since we did not allocate additional data structures, the space complexity is O(1).