Codele / Algorithms
Greedy choices
A greedy method always grabs the best-looking option right now. It is fast and easy, and sometimes it is wrong.
- A greedy method takes the best-looking choice right now and never goes back.
- For coins it means: take the biggest coin that still fits, then repeat.
- It is fast and easy to write, because it never has to compare many possibilities.
- It is not always right. With coins 1, 3, 4, paying 6 greedily gives 4 + 1 + 1, but 3 + 3 is better.
- Before you trust a greedy method, test it on small cases where you can find the best answer by hand.
Loading the playable lesson…