โ† Competitive Programming learning path
Competitive Programming ยท Module 6 of 10

Greedy algorithms

Make the best local choice when it is provably safe.

๐Ÿ“– Key ideas

  1. Greedy picks the best option now and never looks back.
  2. Activity selection: pick the interval that ends earliest.
  3. Coin change is greedy only for some coin systems.
  4. Prove greedy with exchange arguments or counterexamples.
  5. Sorting by a key is often the first greedy step.
๐Ÿ’ก Real-life example: Greedy is like filling a bag with the most valuable items first: great sometimes, wrong when items do not split.
Greedy choiceBest local option now
Exchange argumentProof that swapping does not improve
Activity selectionEarliest finish first
CounterexampleInput where greedy fails