You receive an integer array prices, where prices[i] gives the stock price on day i.
For every day, you can choose whether to purchase and/or sell the stock. You must never own more than one share at once. Buying and then selling that share on the same day is allowed.
Compute the largest possible profit.
Example 1:
Input: prices = [8,2,7,4,9,5]
Output: 10
Explanation:
Purchase on day 2 at price 2 and sell on day 3 at price 7 for a profit of 5. Next, buy on day 4 at price 4 and sell on day 5 at price 9 for another profit of 5. The combined profit is 10.
prices = [8, 2, 7, 4, 9, 5]10
We have stock prices for 6 days. Day 1 price is 8, day 2 is 2, day 3 is 7, day 4 is 4, day 5 is 9, and day 6 is 5.
Example 2:
Input: prices = [2,4,6,8]
Output: 6
Explanation:
Buy on day 1 for 2 and sell on day 4 for 8, producing a profit of 6. This is the same as taking the gain from every consecutive increasing day.
Example 3:
Input: prices = [9,7,5,2]
Output: 0
Explanation:
Every later price is lower, so no trade can earn a positive profit.
1 <= prices.length <= 3 * 10^40 <= prices[i] <= 10^4prices = [8, 2, 7, 4, 9, 5]10
We have stock prices for 6 days. Day 1 price is 8, day 2 is 2, day 3 is 7, day 4 is 4, day 5 is 9, and day 6 is 5.