Best Time to Buy and Sell Stock II
MediumGreedy
Decode it first. Clues, constraints, pattern and plan before any code.prices[i] is a share price on day i. You may buy and sell as many times as you like, but you can hold at most one share at a time. You may buy and sell on the same day. Return the largest total profit you can make.
prices = [7, 1, 5, 3, 6, 4] → 7 (buy 1 sell 5, buy 3 sell 6)
prices = [1, 2, 3, 4, 5] → 4 (buy 1 sell 5, or sell every day)
prices = [7, 6, 4, 3, 1] → 0 (never trade)Constraints
1 <= prices.length <= 3 * 10^40 <= prices[i] <= 10^4