Greedy Algorithms
Build a solution by repeatedly taking the locally best choice — correct only when an exchange argument proves that choice never hurts.
You have not marked these as done yet. Read on anyway if they feel familiar; come back to them if something here does not land.
Overview
A greedy algorithm makes one irrevocable decision at a time using a simple rule ("earliest finishing interval", "cheapest edge", "highest value per weight") and never reconsiders it. When it works it is usually the fastest possible approach — typically a sort followed by a single sweep, O(n log n).
The catch is that greedy is a proof obligation, not a technique: the same local rule that solves Activity Selection optimally gives wrong answers for weighted intervals or 0/1 Knapsack. Before trusting a greedy solution you must argue that a locally best choice can always be extended to a globally optimal one.
Intuition
A mental model before the formal terms.
Imagine paying 63 cents with coins 25, 10, 5, 1: take the biggest coin that fits, repeat. It works because every coin is a multiple of the next smaller one, so a larger coin can always replace a group of smaller ones without loss. With coins {1, 3, 4} and amount 6 the same rule takes 4 + 1 + 1 (three coins) while 3 + 3 (two coins) is optimal — the structure that made greedy safe is gone.
How it works
- Identify the greedy choice: a rule that picks one element using only local information (usually after sorting by some key).
- Show the greedy-choice property: some optimal solution contains that first choice. The standard tool is an exchange argument — take any optimal solution, swap in the greedy choice, and show nothing gets worse.
- Show optimal substructure: after the choice, what remains is a smaller instance of the same problem, so induction finishes the proof.
- Implement as sort + sweep, or with a Priority Queue when the "best remaining" element changes dynamically (Huffman, Dijkstra, Prim).
Why it works
The exchange argument is the whole story. For activity selection: let g be the interval that finishes earliest and o the first interval in some optimal schedule. Since g finishes no later than o, replacing o with g keeps the schedule feasible and the same size — so an optimal schedule starting with g exists, and the remaining problem is the same problem on intervals starting after g ends.
Formally, problems where greedy is always optimal are exactly the matroids (and their generalizations); MST is the canonical example. You do not need the theory in interviews, but you do need a concrete exchange argument for your specific rule.
Recognition
How to tell a problem wants this.
- The statement asks for a maximum count or minimum cost with a natural ordering key (finish time, deadline, ratio, weight).
- Choices do not interact except through a simple resource (time, capacity, one machine): "schedule as many as possible", "minimum number of intervals to remove", "can you reach the end".
- A small example where taking the obvious best choice first is provably fine — and no counterexample after trying to break it.
- Constraints of
n ≤ 10^5with a single sort suggestingO(n log n).
Interactive visualization
Play, step, change the input. ← → and space work too.
1greedyChange(coins, target): # coins sorted descending2 taken = []3 for d in coins:4 while target >= d: # take the biggest coin that fits5 taken.append(d); target -= d6 return taken7# ground truth, for comparison — dynamic programming over every amount:8 best[t] = 1 + min(best[t - d] for d in coins if d <= t)Pseudocode
1sort items by the greedy key2solution = []3for item in items:4 if item is compatible with solution:5 solution.add(item) # never undone6return solutionImplementations
1import math2 3# Greedy: build the answer by repeatedly taking the locally best option.4# This is only correct when an EXCHANGE ARGUMENT holds — that swapping any5# optimal solution toward the greedy choice never makes it worse. The two6# functions below are the same shape; only one of them is correct.7 8 91 · Correct greedy: coin change with a canonical system (1, 5, 10, 25)10def coins_canonical(amount: int, coins: list[int]) -> int:11 count = 012 rest = amount13 for c in coins: # coins sorted descending14 q, rest = divmod(rest, c)15 count += q16 return count if rest == 0 else -117 18 192 · The same greedy is WRONG for a non-canonical system like {1, 3, 4}:20# greedy gives 6 = 4+1+1 (3 coins), the optimum is 3+3 (2 coins)21def coins_dp(amount: int, coins: list[int]) -> int:22 dp = [math.inf] * (amount + 1)23 dp[0] = 024 for a in range(1, amount + 1):25 for c in coins:26 if c <= a and dp[a - c] + 1 < dp[a]:27 dp[a] = dp[a - c] + 128 return -1 if dp[amount] == math.inf else int(dp[amount])29 30 313 · A greedy that IS provable: to cover points with unit intervals, always32# place the interval starting at the leftmost uncovered point33def min_unit_intervals(points: list[int], width: int) -> int:34 ordered = sorted(points)35 used = 036 i = 037 while i < len(ordered):38 used += 139 end = ordered[i] + width # the interval [ordered[i], ordered[i]+width]40 while i < len(ordered) and ordered[i] <= end:41 i += 142 return used43 44 454 · The exchange argument for it: any optimal cover can be rewritten to46# start its leftmost interval at the leftmost uncovered point without using47# more intervals, because that placement covers a superset of what any48# interval covering that point could cover to its right.49 50 515 · The practical test: sort by some key, take greedily, and CHECK against52# brute force on small inputs before trusting it53def greedy_matches_optimal(amount: int, coins_desc: list[int]) -> bool:54 return all(coins_canonical(a, coins_desc) == coins_dp(a, coins_desc) for a in range(amount + 1))divmod(rest, c)returns quotient and remainder in one call, which is both faster and clearer than computing them separately.sorted(points)returns a new list, so the function is side-effect free by default — the opposite oflist.sort().math.infas the DP sentinel needs no overflow guard; the explicitdp[a - c] + 1 < dp[a]comparison also avoids amin()call per candidate.- The
all(... for a in range(...))generator ingreedy_matches_optimalshort-circuits on the first mismatch. int(dp[amount])converts back from the floatmath.infdomain, which is the small cost of usingmath.infin an integer table.
divmod(a, b)is a single call returning both results and is the idiomatic spelling for this pattern.sorted()copies whilelist.sort()mutates — choosing the former is what makesmin_unit_intervalsfree of side effects.math.infis a float, so an integer DP table becomes float-typed; a large integer sentinel keeps itlist[int].hypothesisis the natural tool for the verify-against-brute-force habit this entry advocates, generating small random inputs automatically.
- Using
list.sort()and mutating the caller's list whensorted()was intended. - Trusting a greedy without the cross-check, which is the entire point of the entry.
- Leaving
math.infin a table that downstream code expects to be integers.
- JavaScript is the only language whose default sort actively breaks greedy algorithms: without a comparator it orders numbers lexicographically, so a "sort then take greedily" solution silently takes the wrong things.
- Integer division: Python
divmodgives both results in one call, C++/and%on ints already truncate, and JS/TS needMath.flooraround a float division. - Sentinels:
Infinityandmath.infsaturate safely; C++INT_MAXrequires an explicit reachability guard before any addition. - Sorting side effects: Python
sorted()copies by default andlist.sort()mutates; C++std::sortand JS/TSsortalways mutate, so a copy must be made deliberately.
Complexity
Dominated by the sort; O(n log n) with a heap when the best remaining choice changes dynamically.
Compare growth rates in the Complexity Explorer →When to use — and when not to
- You can state and defend an exchange argument for the greedy choice.
- Interval scheduling, interval partitioning, deadline scheduling, Huffman coding, MST, Dijkstra, fractional knapsack, jump game, gas station.
- The DP formulation exists but the optimal transition is always the same "obvious" one — then greedy is the DP with the search removed.
- 0/1 knapsack: items (weight, value) = (10, 60), (20, 100), (30, 120) with capacity 50 — greedy by ratio takes 160, optimal is 220.
- Coin change with arbitrary denominations ({1, 3, 4}, amount 6).
- Weighted interval scheduling, longest path, or any problem where an early choice constrains later ones in a non-local way — use Dynamic Programming.
- When you cannot find the exchange argument. A greedy that "seems to work on examples" is the most common wrong answer in interviews.
Alternatives
Common mistakes
- Choosing the wrong greedy key (sorting intervals by start time instead of finish time).
- Skipping the proof and shipping a greedy that fails on a hidden case.
- Confusing greedy with DP: DP explores all transitions and keeps the best; greedy commits to one.
- Forgetting ties: several keys equal — pick the tie-break that keeps the exchange argument valid.
Interview patterns
- Sort by finish time, sweep, count compatible intervals.
- Sort by deadline / by ratio, then use a heap to undo the worst earlier choice (job sequencing, "IPO", "maximum performance of a team").
- Farthest-reach greedy: jump game, minimum jumps, video stitching.
- Two-phase "prove greedy then implement": state the exchange argument aloud before coding.
- Merge IntervalsIntermediate
Example problems
Check yourself
Answer out loud or on paper first, then compare. These come from the interview questions linked to this lesson.
A problem looks like it could be greedy. How do you decide whether greedy is correct or whether you need DP?
Show a strong answerHide the answer
Greedy is correct only if the problem has the greedy-choice property: some locally optimal choice is guaranteed to be part of *some* globally optimal solution, and after making it the remaining problem is a smaller instance of the same kind. Both greedy and DP need optimal substructure; the difference is that DP considers every choice at each state and greedy commits to one. So the question is really: can I prove that one specific choice is always safe?
The tool for that proof is the exchange argument: take any optimal solution that does not make the greedy choice, swap in the greedy choice, and show the result is no worse. In Interval Scheduling, picking the interval that ends earliest is safe because any optimal solution's first interval can be replaced by the earliest-ending one without breaking compatibility with the rest. If the exchange argument does not go through, greedy is suspect. The other tool is counterexample hunting: try small adversarial inputs. Coin Change with coins
{1, 3, 4}and amount6— greedy gives4+1+1, optimum is3+3. That one example kills the greedy for arbitrary denominations, though it works for canonical systems like US coins.A strong candidate knows the sibling pairs: Fractional Knapsack is greedy by value density, 0/1 Knapsack is not (density greedy fails when a low-density item fills the capacity exactly); Activity Selection greedy by earliest finish works, weighted interval scheduling needs DP; minimum spanning tree greedy (Kruskal's Algorithm) works because of the cut property, shortest path with negative edges does not. When unsure in an interview, they say so, write the DP (always correct if the state is right), and note that a greedy would be an optimization to prove later — that ordering is the mark of an engineer who values correctness over cleverness.
Full interview question, with follow-ups and red flags →You have written an algorithm. How do you convince me it is correct without running it on every input?
Show a strong answerHide the answer
Testing shows the algorithm works on the inputs tried; a proof shows it works on all of them. A strong candidate matches proof technique to code shape. Loops → invariants. State a property that holds before the first iteration, is preserved by each iteration, and at termination implies the result. For Binary Search: "the target, if present, lies in
[lo, hi]" — true initially, preserved by each discard because of sortedness, and whenlo > hithe range is empty so the target is absent. For Two Pointers (Opposite Ends): "every solution pair lies within[l, r]". For Kadane's Algorithm: "curis the maximum sum of a subarray ending ati". An invariant that cannot be stated precisely is a signal the algorithm might be wrong.Recursion and DP → induction. Assume the recursive calls return correct answers for smaller inputs and show the combination is correct; the base case anchors it. For DP, the induction is over the topological order of states: "
dp[i]is correct assumingdp[j]is correct for alljit depends on". Greedy → exchange argument. Show any optimal solution can be transformed into the greedy one without losing optimality; if the exchange cannot be made, look for the counterexample that the failed exchange suggests. Graph algorithms → structural lemmas. BFS layers equal distances by induction on distance; Dijkstra's correctness rests on the lemma that the minimum tentative node has its final distance, which holds only with non-negative weights.They also use reduction: "this is exactly interval scheduling, whose greedy is known optimal", which transfers a known proof. And they complement the proof with disciplined edge-case enumeration — empty input, single element, all equal, extreme values, the boundary of every
Full interview question, with follow-ups and red flags →<vs<=— because a proof covers the algorithm while edge cases catch the gap between the algorithm and the code. Finally, they say what they are *not* sure of: "I believe the invariant holds when duplicates are present, let me check the<=branch." That honesty is itself evidence of correctness-oriented thinking.Prove earliest-finish-first is optimal for interval scheduling.
Show a strong answerHide the answer
Let
Full interview question, with follow-ups and red flags →Obe optimal with first intervalo_1; greedy picksg_1withend(g_1) ≤ end(o_1). Replacingo_1byg_1keepsOfeasible and the same size. Induct on the remainder afterend(g_1).