Greedy Algorithms8 sections · 316 units
Open in Course

Problem - Best Time to Buy and Sell Stock II

Unlimited transactions

Now you can make as many transactions as you want.

You can buy and sell on the same day, but you cannot hold multiple shares. For prices=[7,1,5,3,6,4]prices = [7,1,5,3,6,4], the maximum profit is 77: buy at 11, sell at 55 (profit 44), buy at 33, sell at 66 (profit 33).

With unlimited transactions, the greedy approach changes.

Think about when it makes sense to "buy" and when to "sell." Every upward price movement is profit.