Dynamic Programming Class Room Notes
Dynamic Programming Class Room Notes
Dynamic Programming is a method for solving complex problems by breaking them down
into simpler subproblems. It is applicable when the problem has:
Optimal Substructure: The optimal solution to a problem can be built from optimal
solutions of its subproblems.
🔹 Key Techniques
1. Memoization (Top-Down)
Solve the problem recursively.
2. Tabulation (Bottom-Up)
Solve all possible subproblems starting from the smallest.
Use an iterative approach to build the solution from the bottom up.
Write the recurrence relation: How is the current state related to previous states?
🔹 Classic Examples
1. Fibonacci Sequence
python
Copy
Edit
# Memoization
def fib(n, memo={}):
if n in memo: return memo[n]
if n <= 1: return n
memo[n] = fib(n-1, memo) + fib(n-2, memo)
return memo[n]
2. 0/1 Knapsack Problem
Choose to include or exclude an item to maximize value within a weight limit.
🔹 Tips
Identify repeated work in recursion → it’s a DP candidate.