Limited time: AI code review, hints, mock interviews, whiteboard analysis, and all Pro features are unlocked. Enroll

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 ⌘⏎
SensAI SensAI
Beta
Listening...
Tap mic to stop voice mode

Unlock AI Features

One click to activate - no payment, no credit card. Just sign in and you're in.

AI code review and hints
SensAI chat assistant
AI mock interviews
Whiteboard analysis
100% free during early access