DSA Trainer
Patterns/Which pattern?
Pattern comparison

Dynamic Programming vs Greedy: how to know which one is safe

Both patterns optimize something: the maximum profit, the minimum number of coins, the longest run. That shared goal is exactly the trap. Greedy is simpler, so beginners reach for it and get a wrong answer on a problem that actually needed DP.

The short answer

Greedy makes the locally best choice and never looks back. DP considers all the choices and remembers the results, so a move that looks worse right now can still win in the end. Greedy is faster, but it's only correct when the best local choice is always part of the best global answer.

The one question that settles it

Ask yourself: if I always grab the best-looking option right now, could I ever regret it later? If yes, you need DP. If you can give a clean argument that the greedy choice is always safe, greedy wins.

Reach for Dynamic Programming when…

  • A choice you make now changes which choices are available later
  • You can't convince yourself the locally-best move is always safe
  • Brute force is exponential and you keep seeing the same subproblem repeat
  • Examples: coin change with arbitrary coins, house robber, edit distance

Reach for Greedy when…

  • You can argue the best local choice never blocks a better global outcome (an exchange argument)
  • Sorting the input and sweeping through once tends to crack it
  • Each step commits and you genuinely never need to reconsider
  • Examples: interval scheduling, activity selection, canonical-coin change

Side by side

Dynamic ProgrammingGreedy
Choice strategyExplore the options, keep the best resultTake the best-looking option right now
Looks back?Yes, it remembers subproblem resultsNever reconsiders a choice
CorrectnessAlways correct if the recurrence is rightCorrect only if the greedy-choice property holds
SpeedSlower, it fills a tableFaster, often one pass after sorting
The dangerOverkill on a truly greedy problemSilently wrong on a problem that needed DP
How to be sureDefine the state and the recurrenceProve the exchange argument

Practice each one

Guided hints, not spoilers. Feel the difference on real problems.

Frequently asked questions

How do I tell if a problem is greedy or DP?
Try to break greedy with a counterexample. Coin change with coins {1, 3, 4} for amount 6: greedy grabs the 4, then 1 + 1, for three coins, but 3 + 3 is two coins. The moment a counterexample exists, greedy is unsafe and you need DP. If you genuinely can't construct one and can argue why, greedy is fair game.
Is greedy always faster than DP?
When it's correct, usually yes: often a single sort-and-sweep versus filling a whole table. The trap is choosing greedy because it's faster and quietly getting wrong answers. Correct-but-slower beats fast-but-wrong every time in an interview.
Can the same problem be solved by both?
Sometimes. Certain problems have both a greedy shortcut and a DP solution; the DP always works, while the greedy works only when its property holds. When you're unsure under pressure, DP is the safe default, since it won't betray you on an edge case.

Stop guessing which pattern to use

The whole point of DSA Trainer: learn to recognize the pattern before you write a line of code. Guided hints, not answers.

More pattern comparisons