Dynamic Programming (DP) is a smart way to solve problems by solving smaller subproblems once and reusing those answers. It is best used when recursion creates repeated work and when the final answer can be built from smaller correct answers.
When to think DP: if recursion tree has repeated nodes (overlapping subproblems), DP is likely useful.
Core conditions: (1) overlapping subproblems, (2) optimal substructure (small optimal answers combine to big optimal answer).
Overlapping subproblems: same subproblem appears multiple times, like fib(3) being solved in different branches.
Optimal substructure: best answer for a big problem can be built from best answers of its smaller subproblems.
Two DP styles: memoization (top-down recursion + cache) and tabulation (bottom-up table filling).
Typical workflow: define state, write recurrence, set base cases, choose storage, then compute answers safely.
Result: for Fibonacci, time improves from O(2^n) to O(n) by avoiding repeated calls.
1) State
State means what one subproblem represents, e.g. dp[n] = value of fib(n).
2) Transition
Transition is the recurrence relation, e.g. dp[n] = dp[n-1] + dp[n-2].
3) Base Case
Base cases anchor recursion/table, e.g. dp[0]=0, dp[1]=1; without them DP breaks.
Approach In Plain English
Recursive Approach (Without DP)
Step 1: To find fib(n), break it into two smaller problems: fib(n-1) and fib(n-2).
Step 2: Each subproblem again breaks into two more subproblems, creating a full recursion tree.
Step 3: Stop only at base cases: fib(0)=0 and fib(1)=1.
Step 4: Add child results while returning back up the tree.
Problem: Same nodes like fib(3) and fib(2) are recalculated multiple times.
DP Memoization Approach
Step 1: Solve recursively in the same style, but keep a memo cache.
Step 2: Before solving fib(k), check if it is already in cache.
Step 3: If cached, return immediately. This prunes the recursion branch.
Step 4: If not cached, compute once and store it for future calls.
Advantage: Every unique subproblem fib(k) is solved only once.
Recursive Walkthrough
Try Any Fibonacci n
Tip: keep n up to 10 for readable trees. Base case is fib(0)=0 and fib(1)=1.
Fibonacci Value: 0
Calls Saved by DP: 0
Without Dynamic Programming (Repeated Work)
Every recursive call expands fully, even if the same subproblem was solved earlier.
def fib(n):
if n <= 1: return n
return fib(n-1) + fib(n-2)
Press Next to start walkthrough.
Without DP Calls: 0
Without DP Step: 0
With DP Memoization (Pruned Tree)
First visit computes a node; repeat visits become cache hits and stop expanding.
def fib_dp(n, memo):
if n in memo: return memo[n]
if n <= 1: return n
memo[n] = fib_dp(n-1,memo) + fib_dp(n-2,memo)
return memo[n]
Press Next to start walkthrough.
With DP Calls: 0
With DP Step: 0
Pseudocode And Complexity
Pseudocode (DP Memoization)
function fib_dp(n, memo):
if n in memo:
return memo[n]
if n <= 1:
return n
left = fib_dp(n - 1, memo)
right = fib_dp(n - 2, memo)
memo[n] = left + right
return memo[n]
main:
memo = empty map
answer = fib_dp(n, memo)
Why first: pseudocode helps understand logic before syntax.
Cache check: prevents repeated recursion on same state.
Recurrence: still follows Fibonacci relation exactly.
Time / Space Complexity
Without DP recursion:
Time : O(2^n) (repeated branches)
Space : O(n) (recursion stack)
With DP memoization:
Time : O(n) (each state computed once)
Space : O(n) (memo + recursion stack)
With tabulation:
Time : O(n)
Space : O(n), can be O(1) with two variables
Big picture: DP reduces recomputation cost dramatically.
Reason: number of unique states is only n+1.
Interview tip: always mention both time and space trade-off.
Implementation (Choose Language)
Language Selector
def fib_recursive(n):
if n <= 1:
return n
return fib_recursive(n - 1) + fib_recursive(n - 2)
def fib_dp(n, memo=None):
if memo is None:
memo = {}
if n in memo:
return memo[n]
if n <= 1:
return n
memo[n] = fib_dp(n - 1, memo) + fib_dp(n - 2, memo)
return memo[n]
int fib_recursive(int n) {
if (n <= 1) return n;
return fib_recursive(n - 1) + fib_recursive(n - 2);
}
int fib_dp(int n, int memo[]) {
if (memo[n] != -1) return memo[n];
if (n <= 1) return n;
memo[n] = fib_dp(n - 1, memo) + fib_dp(n - 2, memo);
return memo[n];
}
Line-by-Line Explanation
if memo is None: creates cache only once per top-level call.
if n in memo: immediately returns stored value, skipping recursion.
if n <= 1: handles base cases to stop recursion safely.
memo[n] = ...: stores solved state for future reuse.
return memo[n]: returns cached final result for this subproblem.
memo[n] != -1: means the state is already computed.
n <= 1: base case for Fibonacci, same as math definition.
Recursive calls: compute left and right subproblems once per new state.
memo[n] = ...: stores result in array to avoid repeat work.
Initialization: fill memo with -1 before first call.