Greedy Algorithms8 sections · 316 units
Open in Course

Lesson from Coin Change

Know your input

The same problem (minimum coins) requires different algorithms depending on the input:

1.1. Standard coins (US, Euro): greedy works

2.2. Arbitrary coins: need DP Here's a pattern you will see often. Greedy works for restricted inputs, DP handles general cases. Before coding, ask: does this input have special structure that makes greedy work? If unsure, test greedy on small examples. A single counterexample proves greedy fails. Common trap: forcing greedy on arbitrary inputs because it worked on standard ones. Always test greedy on custom examples before submitting.