Greedy Algorithms
Making the locally-best choice at each step and never revisiting it — correct only when the problem has the right structural guarantee.
What it is
A greedy algorithm builds a solution one step at a time, always taking the choice that looks best right now, and never reconsiders it. That's fast (usually O(n log n) or O(n)) but only correct on problems with a specific structure.
When greedy actually works
A greedy choice is provably correct when the problem has the greedy-choice property (a locally optimal choice is part of some globally optimal solution) and optimal substructure (an optimal solution contains optimal solutions to subproblems). Interval scheduling (pick the meeting that ends earliest first) is a classic example that satisfies both.
When it doesn't
0/1 knapsack looks similar to greedy problems but isn't — taking the highest value-per-weight item first can lock you out of a better combination, which is why it needs dynamic programming instead.
The tell
If you can prove "the earliest/smallest/cheapest choice is never wrong" with an exchange argument, greedy applies. If you can construct a counterexample where the locally-best choice leads to a worse overall outcome, it doesn't — and you likely need DP.
