Algorithms

Dynamic Programming

๐Ÿ“‹ Quick Reference

PropertyValue
TypeOptimization technique
Key IdeaBreak into subproblems, store results, avoid recomputation
RequirementsOptimal substructure + overlapping subproblems
ApproachesTop-down (memoization) or Bottom-up (tabulation)
Best ForOptimization problems, counting problems
One-liner: Solve complex problems by breaking into overlapping subproblems and caching results.

๐ŸŽฎ Interactive Visualizer

Watch how DP builds solutions from smaller subproblems:

Loading visualizer...
Try these operations:
  1. See Fibonacci with memoization - avoid redundant calls
  2. Watch the DP table fill for knapsack
  3. Observe how subproblems build on each other
  4. Compare top-down vs bottom-up approaches

๐Ÿ”ง Classic Problems

Fibonacci (Top-Down with Memoization)

JAVA(12 lines)
Code
Loading syntax highlighter...

Fibonacci (Bottom-Up with Tabulation)

JAVA(27 lines)
Code
Loading syntax highlighter...

0/1 Knapsack

JAVA(19 lines)
Code
Loading syntax highlighter...

Longest Common Subsequence (LCS)

JAVA(16 lines)
Code
Loading syntax highlighter...

Coin Change (Minimum Coins)

JAVA(15 lines)
Code
Loading syntax highlighter...

๐Ÿ“Š DP Patterns

Pattern 1: Linear DP

JAVA(13 lines)
Code
Loading syntax highlighter...

Pattern 2: 2D Grid DP

JAVA(18 lines)
Code
Loading syntax highlighter...

Pattern 3: String DP

JAVA(24 lines)
Code
Loading syntax highlighter...

Pattern 4: Interval DP

JAVA(23 lines)
Code
Loading syntax highlighter...

โœ… When to Use DP

Good Indicators

  • "Minimum/maximum" - optimization problem
  • "Count the ways" - counting problem
  • "Can it be done?" - feasibility problem
  • Overlapping subproblems - same states computed repeatedly
  • Optimal substructure - optimal solution uses optimal sub-solutions

Common DP Problems

CategoryExamples
SequenceFibonacci, Climbing Stairs, LIS
GridUnique Paths, Minimum Path Sum
StringLCS, Edit Distance, Palindrome
Knapsack0/1 Knapsack, Coin Change
IntervalMatrix Chain, Burst Balloons
TreeTree DP, Binary Tree Maximum Path

๐Ÿ”„ Top-Down vs Bottom-Up

AspectTop-Down (Memoization)Bottom-Up (Tabulation)
DirectionStart from problem, recurseStart from base cases
ComputationOnly needed subproblemsAll subproblems
StackRecursion overheadIterative
DebuggingEasier to understandEasier to trace
Space OptimizationHarderOften possible
JAVA(16 lines)
Code
Loading syntax highlighter...

โš ๏ธ Common Pitfalls

1. Wrong Base Case

JAVA(10 lines)
Code
Loading syntax highlighter...

2. Wrong Recurrence Relation

JAVA(5 lines)
Code
Loading syntax highlighter...

3. Index Out of Bounds

JAVA(5 lines)
Code
Loading syntax highlighter...

4. Not Recognizing DP Problem

JAVA(6 lines)
Code
Loading syntax highlighter...

๐ŸŽค Interview Tips

Q: How do you identify a DP problem?
"

Look for optimization/counting problems with overlapping subproblems. If you see the same recursive calls being made, it's likely DP. Common patterns: "minimum/maximum", "count ways", "is it possible".

Q: Top-down or bottom-up?
"

Top-down is often easier to derive (just add memoization to recursion). Bottom-up can be more efficient (no recursion overhead) and easier to optimize space. Start with top-down, convert to bottom-up if needed.

Q: How do you derive the recurrence?
"

Think about the last decision/step. What choices lead to the current state? Express the answer in terms of smaller subproblems. Draw decision trees if stuck.

Q: How do you optimize DP space?
"

If dp[i] only depends on dp[i-1] and dp[i-2], you only need two variables, not an array. For 2D DP, if you only look at the previous row, use a 1D array.


๐Ÿ“š Series Navigation


Visualizer: DP from @tomaszjarosz/react-visualizers