Problem · Array
Best Time to Buy and Sell Stock II
Learn this problemProblem statement
Given daily prices, return the maximum profit obtainable by any number of buy-then-sell transactions while holding at most one share at a time.
Function
maxProfit(prices: int[]) → intExamples
Example 1
prices = [7,1,5,3,6,4]return = 7Take the rises from one to five and from three to six.
Example 2
prices = [1,2,3,4,5]return = 4The entire increasing run yields four.
Constraints
0 <= prices.length <= 1000000 <= prices[i] <= 100000