Greedy Algorithms8 sections · 316 units
Open in Course

Stock II - Greedy Insight

Collect all gains

The insight: collect every positive difference.

If tomorrow's price is higher than today's, you profit by buying today and selling tomorrow.

You do not need to find optimal buy-sell pairs. Just capture every upward movement. Buy at every local minimum, sell at every local maximum. Or equivalently: add up all positive day-to-day differences.

This greedy choice works because transactions are free and unlimited. There is no penalty for trading often.