Best Time to Buy and Sell Stock II

Medium

Topics
ArrayGreedyDynamic Programming

You are given an array prices where prices[i] is the price of a stock on day i. You may buy and sell as many times as you like (but hold at most one share at a time). Return the maximum profit.

Example 1

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

Example 2

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

Constraints

  • 1 <= prices.length <= 3*10^4
  • 0 <= prices[i] <= 10^4
Run ⌘' · Submit ⌘⏎