A greedy algorithm builds an answer step by step, always grabbing the option that looks best right now. It never revisits a past choice. This makes it fast and simple, usually O(n log n) after a sort, sometimes O(n).
The greedy choice works when two things hold. First, a locally best pick is part of some overall best answer (the greedy-choice property). Second, solving what remains after that pick gives the full solution (optimal substructure).
Making change with coin sizes like 1, 5, 10, 25 works greedily: take the biggest coin that fits, repeat. It fails on odd coin sets, where a smaller first coin wins.
When you cannot prove the pick is safe, greedy quietly returns a wrong answer. Test it against a slower exact method before trusting it.
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 ↓