The exchange argument is the most powerful and elegant technique for proving greedy algorithms correct.
It's used to prove interval scheduling, activity selection, Huffman coding, and dozens of other greedy algorithms. Once you master it, you can prove correctness for an entire class of problems.
This guide teaches you the exchange argument: what it is, the step-by-step template, detailed examples, and when to use it.
TL;DR
Exchange Argument: Show that any optimal solution can be transformed to include the greedy choice, without making it worse.
Key idea: "Exchange" a choice in the optimal solution with the greedy choice. If the result is still optimal, the greedy choice is safe.
Template:
- Let G = greedy solution, O = optimal solution
- Find first difference between G and O
- Exchange: Replace element in O with greedy choice
- Prove exchange maintains optimality
- Repeat until G = O
- Conclude: greedy is optimal
What Is the Exchange Argument?
The Core Idea
Problem: How do we prove a greedy algorithm produces an optimal solution?
Naive approach: Try to prove greedy directly builds optimal solution.
Challenge: Hard to prove without knowing what optimal looks like.
Exchange argument approach:
- Assume some optimal solution exists (don't need to know what it is)
- Show we can "exchange" parts of it to match the greedy solution
- Prove each exchange maintains optimality
- Conclude: greedy solution is also optimal
Why It Works
Key insight: If we can transform any optimal solution into the greedy solution without making it worse, then the greedy solution must be optimal.
Analogy: Imagine two paths to the top of a mountain (both optimal). If you can gradually transform one path into the other without losing elevation, both paths must reach the same height.
Step-by-Step Template
The Universal Template
1. Setup
- Let G = {g₁, g₂, ..., gₖ} be the greedy solution
- Let O = {o₁, o₂, ..., oₘ} be an optimal solution
- Goal: Show k = m (greedy has same size as optimal)
2. Base Case
- If g₁ = o₁, both solutions start the same ✓
3. Exchange Step
- If g₁ ≠ o₁, replace o₁ with g₁ in O
- Get O' = {g₁, o₂, ..., oₘ}
4. Prove O' is Valid
- Show O' satisfies all constraints
- Show no conflicts introduced
5. Prove O' is Optimal
- Show |O'| = |O| (same size/value)
- Therefore O' is also optimal
6. Induction
- O' now starts with g₁ (same as greedy)
- Repeat for g₂, g₃, ...
- Eventually O' = G
7. Conclusion
- Greedy solution can be made identical to optimal
- Therefore greedy is optimal ✓Example: Interval Scheduling (Detailed)
Problem Setup
Input: Activities with start and end times
Goal: Select maximum non-overlapping activities
Greedy: Sort by end time, pick earliest-ending
The Proof
Step 1: Setup
G = {g₁, g₂, ..., gₖ} (greedy solution, sorted by end time)
O = {o₁, o₂, ..., oₘ} (optimal solution, sorted by end time)
Goal: Show k = mStep 2: Base Case
If g₁ = o₁:
Both solutions start with same activity ✓
Continue to next activityStep 3: Exchange
If g₁ ≠ o₁:
Since g₁ has earliest end time: end(g₁) ≤ end(o₁)
Replace o₁ with g₁ in O
O' = {g₁, o₂, ..., oₘ}Step 4: Prove O' is Valid
Need to show: g₁ doesn't conflict with o₂, o₃, ..., oₘ
Proof:
- In O, o₁ doesn't conflict with o₂, ..., oₘ
- This means: end(o₁) ≤ start(o₂)
- Since end(g₁) ≤ end(o₁):
end(g₁) ≤ end(o₁) ≤ start(o₂)
- Therefore: g₁ doesn't conflict with o₂
- Same argument for o₃, ..., oₘ
- O' is valid ✓Step 5: Prove O' is Optimal
|O'| = |O| = m (same number of activities)
O is optimal, so O' is also optimal ✓Step 6: Induction
O' now starts with g₁ (same as greedy)
Remaining problem: activities after g₁
Repeat exchange for g₂, g₃, ...
Eventually O' = GStep 7: Conclusion
|G| = |O'| = |O| = m
Greedy produces optimal solution ✓Visual Representation
Optimal O: [----o₁----] [--o₂--] [--o₃--]
↓ exchange g₁ for o₁
Modified O': [--g₁--] [--o₂--] [--o₃--]
g₁ ends earlier than o₁, so no new conflicts.
O' is still optimal (same size).
Repeat: [--g₁--] [--g₂--] [--o₃--]
↓ exchange g₂ for o₂
[--g₁--] [--g₂--] [--g₃--]
Eventually O' = G (greedy solution).
Therefore greedy is optimal ✓Example: Task Scheduling
Problem Setup
Input: Tasks with deadlines and penalties
Goal: Minimize total penalty for late tasks
Greedy: Sort by penalty (highest first), schedule in order
The Proof (Sketch)
Step 1: Setup
- G = greedy schedule (sorted by penalty)
- O = optimal schedule
Step 2: Exchange
- If G ≠ O, find first task that differs
- Let g be greedy choice (highest penalty among remaining)
- Let o be task in that position in O
- penalty(g) ≥ penalty(o) (by greedy choice)
Step 3: Exchange g and o
- Swap positions of g and o in O
- If g is late in new position: penalty increases by penalty(g)
- If o is late in new position: penalty increases by penalty(o)
- Since penalty(g) ≥ penalty(o), total penalty doesn't increase
Step 4: Conclusion
- Exchange maintains or improves optimality
- Repeat until O = G
- Greedy is optimal ✓
When to Use Exchange Argument
✅ Use Exchange Argument When:
Selection problems
- Selecting subset of items
- Maximizing/minimizing count
Scheduling problems
- Activity selection
- Task scheduling
- Interval scheduling
Greedy builds solution incrementally
- Each step makes one choice
- Choices are independent
Can define "first difference"
- Easy to identify where greedy and optimal differ
- Can swap elements
❌ Don't Use When:
State-based problems
- Use induction instead
- Example: Jump Game
Accumulation problems
- Use direct proof
- Example: Gas Station
Can't identify clear exchange
- Use greedy choice property
- Or find counterexample
Common Mistakes
Mistake 1: Not Proving Exchange Maintains Validity
❌ Wrong:
"Replace o₁ with g₁"
(Doesn't check if g₁ conflicts with o₂, o₃, ...)✅ Correct:
"Replace o₁ with g₁
Prove: g₁ doesn't conflict with o₂, ..., oₘ
Because: end(g₁) ≤ end(o₁) ≤ start(o₂)" ✓Mistake 2: Not Proving Exchange Maintains Optimality
❌ Wrong:
"Replace o₁ with g₁, now O' is better"
(Doesn't prove O' is still optimal)✅ Correct:
"Replace o₁ with g₁
|O'| = |O| (same size)
O is optimal, so O' is optimal" ✓Mistake 3: Forgetting Induction Step
❌ Wrong:
"Exchanged first element, done"
(What about remaining elements?)✅ Correct:
"Exchanged first element
Repeat for second, third, ...
Eventually O' = G
Therefore greedy is optimal" ✓Practice Problems
Master exchange argument with these:
- Non-overlapping Intervals (#435) - classic exchange argument
- Minimum Arrows (#452) - similar to interval scheduling
- Task Scheduler (#621) - scheduling with constraints
FAQ
Q: When should I use exchange argument vs greedy choice property?
A: Exchange argument is more formal and rigorous. Use it for interval/selection problems. Greedy choice property is more intuitive, use for initial understanding.
Q: Do I need to write full proof in interviews?
A: Explain the key insight: "Any optimal solution can be transformed to include the greedy choice without making it worse." Full mathematical proof not required.
Q: What if I can't find an exchange?
A: Try greedy choice property or induction. If those fail, greedy might not work—look for counterexample.
Conclusion
The exchange argument is the gold standard for proving greedy correctness.
Key principles:
- Transform optimal to greedy via exchanges
- Each exchange maintains optimality
- Eventually optimal = greedy
- Therefore greedy is optimal
The template:
- Setup: G (greedy), O (optimal)
- Find first difference
- Exchange element in O with greedy choice
- Prove exchange maintains validity and optimality
- Repeat until O = G
- Conclude: greedy is optimal
Master this technique, and you'll prove greedy correctness with confidence. For more proof techniques, see Greedy Proof Techniques, Greedy Choice Property, and the complete greedy guide.
Next time you need to prove greedy correctness, reach for the exchange argument.
Want to Practice LeetCode Smarter?
LeetCopilot is a free browser extension that enhances your LeetCode practice with AI-powered hints, personalized study notes, and realistic mock interviews — all designed to accelerate your coding interview preparation.
Also compatible with Edge, Brave, and Opera
