Dynamic Programming
Dynamic Programming
Common DP Problems:
1. Fibonacci numbers.
2. Longest Common Subsequence (LCS).
3. Longest Increasing Subsequence (LIS).
4. Knapsack problem.
5. Matrix Chain Multiplication.
6. Coin Change problem.
7. Minimum Edit Distance.
Top-Down Approach (Memoization)
The Top-Down approach starts from the problem's largest instance and recursively breaks it
into smaller subproblems. It uses recursion and a cache (or memo table) to store the results of
subproblems so they don’t have to be recomputed. The working of Memoization is as
follows:
Let us understand the Top-Down Approach by using the Fibonacci problem. We need to find
the n-th Fibonacci number where:
F(0)=0
F(1)=1
F(n)=F(n−1)+F(n−2) for n≥2
For this example, let’s calculate F(6). Now consider the following steps:
Calculations:
F(6) = F(5)+F(4)
Break further:
Memoization Table:
n F(n)
0 0
1 1
2 1
3 2
4 3
5 5
6 8
The Bottom-Up approach solves the problem iteratively by starting from the smallest
subproblems (base cases) and building up the solution to the original problem. It uses a table
to store results. The working of Tabulation is as follows:
Let us understand the Bottom-Up Approach by using the Fibonacci problem. We need to find
the n-th Fibonacci number where:
Base 0 1
1 0 1 1
2 0 1 1 2
3 0 1 1 2 3
4 0 1 1 2 3 5
5 0 1 1 2 3 5 8
F(6)=8
The coin change problem is a classic dynamic programming problem. You are given a set of
coins of different denominations and a target amount. Your task is to determine the minimum
number of coins required to make up the target amount. If it’s not possible to make up the
amount, return -1.
dp[i]=min(1+dp[i−c])
i: denomination
c: coin
Example 1
Coins: [1,2,5]
Target Amount: 11
11=5+5+1 (3 coins)
Example 2
Coins: [2]
Target Amount: 3
−1. It is impossible to make 3 using only coins of denomination 2.
Example 1:
Coins: [1,2,5]
Target: 11
Goal: Determine the minimum number of coins needed to make each amount i from 0 to
11 using the coins 1,2,5
i=1
i=2
Using 1: 1+dp[2−1]=1+1=2.
Using 2: 1+dp[2−2]=1+0=1. (1 coin of 2 suffices.)
Using 5: Not possible (amount 2−5<0).
dp[2]=min(2,1,∞)=1.
i=3
Using 1: 1+dp[3−1]=1+1=2.
Using 2: 1+dp[3−2]=1+1=2.
Using 5: Not possible (amount 3−5<0).
dp[3]=min(2,2,∞)=2.
i=4
Using 1: 1+dp[4−1]=1+2=3.
Using 2: 1+dp[4−2]=1+1=2
Using 5: Not possible (amount 4−5<0).
dp[4]=min(3,2,∞)=2.
i=5
Using 1: 1+dp[5−1]=1+2=3.
Using 2: 1+dp[5−2]=1+2=3.
Using 5: 1+dp[5−5]=1+0=1
dp[5]=min(3,3,1)=1
i=6
Using 1: 1+dp[6−1]=1+1=2.
Using 2: 1+dp[6−2]=1+2=2.
Using 5: 1+dp[6−5]=1+1=2.
dp[6] = min(2,2,2)=2.
i=7
Using 1: 1+dp[7−1]=1+2=3.
Using 2: 1+dp[7−2]=1+1=2.
Using 5: 1+dp[7−5]=1+2=2.
dp[7]=min(3,2,2)=2.
i=8
Using 1: 1+dp[8−1]=1+2=3.
Using 2: 1+dp[8−2]=1+2=3.
Using 5: 1+dp[8−5]=1+1=2.
dp[8]=min(3,3,2)=2
i=9
Using 1: 1+dp[9−1]=1+2=3.
Using 2: 1+dp[9−2]=1+2=3.
Using 5: 1+dp[9−5]=1+2=3.
dp[9]=min(3,3,3)=3
i=10
Using 1: 1+dp[10−1]=1+3=4.
Using 2: 1+dp[10−2]=1+2=3.
Using 5: 1+dp[10−5]=1+1=2.
dp[10]=min(4,3,2)=2
i=11
Using 1: 1+dp[11−1]=1+2=3.
Using 2: 1+dp[11−2]=1+3=4.
Using 5: 1+dp[11−5]=1+2=3.
dp[11]=min(3,4,3)=3
dp=[0,1,1,2,2,1,2,2,2,3,2,3]