A greedy algorithm builds an answer step by step, always taking the option that looks best. It never revisits a choice, which keeps it fast and simple, usually O(n log n) after a sort, sometimes O(n).
Greedy works when two things hold. The first is the greedy-choice property, where a locally best pick is part of some overall best answer. The second is optimal substructure, where solving what remains after that pick gives the full solution.
Making change in US coins works greedily. You take the biggest coin that fits, 25 cents first, then 10, 5 and 1. It fails on unusual coin sets, where a smaller first coin wins.
An unproven greedy pick can return a wrong answer with no error, so test it against a slower exact method.
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
The diagram below the answer is the concept . Jump to it ↓