Skip to content
Veltoryx Technologies
ProProblem 5● MediumLeetCode #122 ↗2 min readUpdated 30 October 2026

Best Time to Buy and Sell Stock II

With unlimited trades, collect every day-to-day price rise. A simple greedy idea that is provably best.

01The problem

Same prices as before, but now you may trade as many times as you like (holding at most one share at a time). Return the biggest total profit.

InputOutputWhy
[7, 1, 5, 3, 6, 4]7buy 1 sell 5 (+4), buy 3 sell 6 (+3)
[1, 2, 3, 4, 5]4buy 1, sell 5
[7, 6, 4, 3, 1]0no rises

Loading the rest of the lesson…