Skip to content
← All Comparisons

Greedy vs Dynamic Programming

Quick Answer

Use Greedy when making the locally optimal choice at each step provably leads to the global optimum (the problem has the greedy choice property). Use Dynamic Programming when a greedy approach fails because the problem has overlapping subproblems and you need to consider all possibilities to find the optimum.

Side-by-Side Comparison

AspectGreedyDynamic Programming
ApproachMake the best local choice at each stepSolve all subproblems, combine for global optimum
GuaranteeOptimal only if greedy choice property holdsAlways optimal (if formulated correctly)
Time complexityUsually O(n log n) with sorting, or O(n)O(n * W) or O(n^2) depending on problem
Space complexityO(1) to O(n)O(n) to O(n * W) for DP table
Backtracking needed?No (never revisits decisions)No (but considers all options via subproblems)
Required propertiesGreedy choice property + optimal substructureOverlapping subproblems + optimal substructure
ImplementationSort + iterate (usually simpler code)Memoization or tabulation (more complex code)

When to Use Greedy

When to Use Dynamic Programming

Key Differences

Decision permanence

Greedy commits irrevocably to each choice and never looks back. DP explores all possibilities by solving every subproblem and combining results. This is why greedy is faster but only works when the greedy choice property holds.

How to recognize each

Greedy: Can you prove that always picking the "best" option at each step leads to the overall best solution? If yes, greedy works. DP: Can you find a counterexample where greedy fails? If yes, you need DP. Also, if the problem asks "count the number of ways", greedy almost never applies.

Complexity trade-off

Greedy algorithms are typically O(n log n) or O(n) and use minimal extra space. DP algorithms often require O(n^2) or O(n * W) time and space for the DP table. When greedy works, it is the superior approach.

How to Decide

  1. Try greedy first. It is simpler and faster. Formulate a greedy strategy (e.g., always pick the largest/smallest/earliest).
  2. Look for a counterexample. Can you construct an input where the greedy strategy gives a wrong answer? If yes, you need DP.
  3. Does the problem ask "count the number of ways"? Almost always DP (greedy gives one solution, not a count).
  4. Does the problem involve making a sequence of dependent decisions? If choosing option A now affects what options are available later, consider DP.
  5. Is there a known greedy proof? Classic greedy problems (activity selection, fractional knapsack, Huffman) have well-known proofs.
  6. Does the problem resemble a known DP pattern? Knapsack, LCS, edit distance, coin change with min coins — these all require DP.

Example Problems

Greedy

  • 1.Jump Game — Track the farthest reachable index; greedily extend the reach at each step.
  • 2.Activity Selection — Sort by end time, greedily pick the next non-overlapping activity.
  • 3.Assign Cookies — Sort children by greed and cookies by size, assign the smallest sufficient cookie.

Dynamic Programming

  • 1.Coin Change — Find min coins to make amount. Greedy (largest coin first) fails for non-standard denominations.
  • 2.Longest Increasing Subsequence — Greedy picks the smallest next element but misses longer subsequences found by skipping.
  • 3.Edit Distance — No greedy rule exists; you must explore insert, delete, and replace at each position.

Problems That Look Greedy But Need DP

  • 1.Coin Change (non-standard denominations) — With coins [1, 3, 4] and target 6, greedy picks 4+1+1=3 coins, but optimal is 3+3=2 coins. You must try all combinations via DP.
  • 2.0/1 Knapsack — Unlike fractional knapsack (greedy works), 0/1 knapsack requires DP because you cannot take partial items. Greedily picking the best value/weight ratio can miss the optimal selection.