▶  Watch

Greedy vs Dynamic Programming: Grab the Best Now, or Plan the Whole?

Making change for 6 with coins {1, 3, 4}: greedy grabs the biggest coin that fits each time and never looks back, landing on 4+1+1. Dynamic programming solves every overlapping subproblem once and remembers the best answer, finding the true optimum of 3+3.

Algorithms CS Fundamentals
What this teaches

Greedy repeatedly makes the locally best choice and never reconsiders it — fast and simple, but only correct when the local best choice is provably safe (Dijkstra, Huffman coding). Dynamic programming breaks a problem into overlapping subproblems, solves each exactly once, and remembers the answer to build the true optimum — slower and more memory-hungry, but it can't be fooled by a tempting-but-wrong local choice. The coin-change example makes it concrete: with coins {1, 3, 4} and a target of 6, greedy finds 4+1+1 (three coins) while DP finds the actual optimum, 3+3 (two coins).

Transcript

You're paying with coins and want to use as few as possible. Do you just grab the biggest coin that fits each time — or plan the whole combination first? That's greedy versus dynamic programming: grab now, or plan the whole. One is fast but easily fooled; the other is bulletproof but costs more.

Greedy is simple: at each step, take whatever looks best RIGHT NOW, and never look back. To make change, grab the biggest coin that fits, then repeat. It's fast, it's obvious, and for many problems it's exactly right — as long as the locally best choice always leads to the globally best answer.

Dynamic programming won't just grab the biggest coin. It breaks the amount into smaller subproblems, solves each once, and REMEMBERS the fewest-coins answer for each. Then it builds the best combination for the whole amount from those saved pieces. Slower, and more memory — but it can't be fooled by a tempting first grab.

Here's where greedy bites. Make 6 cents from coins worth 1, 3, and 4. Greedy grabs the 4, then a 1, then a 1 — that's three coins. But 3 plus 3 is just TWO coins. Greedy took the obvious first step and never reconsidered. DP checks the combinations and finds the real best.

So use greedy when you can PROVE the local choice is always safe — it's faster and simpler. Many classic algorithms are greedy for exactly that reason. Reach for DP when a tempting early choice can sabotage the whole — but the subproblems overlap, so remembering their answers pays off big.

Same goal, two mindsets: greedy grabs the best step and hopes it adds up; DP remembers every sub-answer to guarantee that it does. So next time you're optimizing, ask one thing: can I safely just grab the best right now — or will that obvious move quietly cost me later?

← All videos · Vibe Engines · 2026

Explore the topic

See this challenge alongside everything else on the same subject — handbooks, system designs, algorithms and tools, in one place.