Best Time to Buy and Sell Stock II

Implement maxProfit

Given the daily `prices` of a stock, you may buy and sell as many times as you like — but you can never hold more than one share at a time, so you must sell (or already be holding nothing) before buying again. Maximize the total profit across every trade you make. The single-transaction version of this problem tracks one running minimum and one running best-so-far. This version removes the "only once" restriction, which changes the shape of the answer entirely: with unlimited trades and no cost per trade, the best possible total is simply the sum of every day-to-day price increase in the whole sequence. Any stretch where the price climbs for several days in a row contributes the same total either way — bought once at the bottom and sold once at the top, or split into several smaller back-to-back trades along the way — so there's no benefit to ever holding through a day where the price is about to drop.

Example 1:

Input: prices = [7,1,5,3,6,4]

Output: 7

Example 2:

Input: prices = [1,2,3,4,5]

Output: 4

Example 3:

Input: prices = [7,6,4,3,1]

Output: 0

+ 7 hidden test cases run on Submit.

Constraints:

  • 1 ≤ prices.length ≤ 12
  • 0 ≤ prices[i] ≤ 1000

prices =

[7, 1, 5, 3, 6, 4]