Algorithms · Dynamic Programming Problem Set: LIS, Edit Distance, Coin Change, Rod Cutting and Counting Strings
Edit Distance (Levenshtein Distance)
Allowed operations: insert, delete, replace, each cost 1.
State: = min operations to convert into .
Recurrence:
Symbols: = delete ; = insert ; = replace.
Answer: . Time/space ; length only can be done in space with two rows.
3.1 Full table: "kitten", "sitting" (, )
Base row/column: , .
| ε | s | i | t | t | i | n | g | |
|---|---|---|---|---|---|---|---|---|
| ε | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| k | 1 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| i | 2 | 2 | 1 | 2 | 3 | 4 | 5 | 6 |
| t | 3 | 3 | 2 | 1 | 2 | 3 | 4 | 5 |
| t | 4 | 4 | 3 | 2 | 1 | 2 | 3 | 4 |
| e | 5 | 5 | 4 | 3 | 2 | 2 | 3 | 4 |
| n | 6 | 6 | 5 | 4 | 3 | 3 | 2 | 3 |
Let me verify a few cells by hand:
- : k vs s differ → . ✔
- : i vs i equal → . ✔
- : t vs t equal → . ✔
- : n vs g differ → . ✔
Answer = 3. (kitten → sitten → sittin → sitting.)
3.2 Relation to LCS
With replace allowed, the relation is not a simple closed form; the standard identity holds for the insert/delete-only variant. GATE sometimes asks the insert/delete-only version — read the operation list carefully.
Shortest Common Supersequence (SCS):
Longest Palindromic Subsequence (LPS):
Example: "character" (c h a r a c t e r). The longest palindromic subsequence is "carac" (positions 1, 3, 4, 5, 6), so .
3.3 LCS table recap (one exact example)
"ABCBDAB" (), "BDCABA" (). Recurrence: if , else .
| ε | B | D | C | A | B | A | |
|---|---|---|---|---|---|---|---|
| ε | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| A | 0 | 0 | 0 | 0 | 1 | 1 | 1 |
| B | 0 | 1 | 1 | 1 | 1 | 2 | 2 |
| C | 0 | 1 | 1 | 2 | 2 | 2 | 2 |
| B | 0 | 1 | 1 | 2 | 2 | 3 | 3 |
| D | 0 | 1 | 2 | 2 | 2 | 3 | 3 |
| A | 0 | 1 | 2 | 2 | 3 | 3 | 4 |
| B | 0 | 1 | 2 | 2 | 3 | 4 | 4 |
Spot checks: (B vs B): equal → ✔. (B vs A): ✔.
LCS length = 4 (e.g. "BCBA"). Time/space ; length only in space.
Coin Change
Two different problems — never mix their recurrences.
4.1 Minimum number of coins (unlimited supply)
State: = min coins to make amount .
Recurrence:
Answer: . Time where = number of denominations.
Example: coins , .
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | |
|---|---|---|---|---|---|---|---|
| 0 | 1 | 2 | 1 | 1 | 2 | 2 |
Check : (coins 3+3). ✔ Note greedy (4+1+1=3) is not optimal — this is the classic greedy-fails example.
4.2 Number of ways (unlimited supply, order does not matter)
State: = number of combinations summing to .
Recurrence (iterate coins outer, amounts inner):
Example: coins , .
| Coin processed | |||||
|---|---|---|---|---|---|
| init | 1 | 0 | 0 | 0 | 0 |
| c=1 | 1 | 1 | 1 | 1 | 1 |
| c=2 | 1 | 1 | 2 | 2 | 3 |
| c=3 | 1 | 1 | 2 | 3 | 4 |
Answer = 4 (1+1+1+1, 1+1+2, 2+2, 1+3). ✔
Order matters (permutations)? Swap loops: amounts outer, coins inner — then counts ordered sequences. For , : , , , , . (Sequences: 1111,112,121,13,211,22,31 = 7.) ✔
4.3 0/1 coin change (each coin used at most once)
State: = ways using first coins to make . Iterate downward in 1-D to avoid reuse. This is exactly subset-sum counting.
Rod Cutting
State: = max revenue from a rod of length ; prices .
Recurrence:
Answer: . Time , space .
Example: for lengths 1..8.
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | |
|---|---|---|---|---|---|---|---|---|
| 1 | 5 | 8 | 10 | 13 | 17 | 18 | 22 |
Check : ✔ (2+2). Check : ✔ (2+3). Check : ✔ (2+6 or 6+2).
0/1 Knapsack Recap (pseudo-polynomial)
State: = max value using first items with capacity .
Recurrence:
Answer: . Time — pseudo-polynomial because is a value, not input length; the input size is bits, so the algorithm is exponential in input size. This is why 0/1 knapsack is NP-hard yet has this DP.
Example: items : (1,1),(3,4),(4,5),(5,7), .
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | |
|---|---|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 1 (1,1) | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
| 2 (3,4) | 0 | 1 | 1 | 4 | 5 | 5 | 5 | 5 |
| 3 (4,5) | 0 | 1 | 1 | 4 | 5 | 6 | 6 | 9 |
| 4 (5,7) | 0 | 1 | 1 | 4 | 5 | 7 | 8 | 9 |
Check : ✔. : ✔. Answer 9.
Subset Sum and Partition
Subset sum: does some subset of sum to ? Boolean table, time, space (iterate downward).
Partition: can the array be split into two equal-sum halves? Answer yes iff total sum is even and subset-sum with is true.
Example: , , . Subset or → yes. ✔ Example: , odd → no. ✔
8.1 Binary strings of length with no two consecutive 0s
Let = count. (0,1), (01,10,11). Recurrence (Fibonacci).
| 1 | 2 | 3 | 4 | 5 | 6 | |
|---|---|---|---|---|---|---|
| 2 | 3 | 5 | 8 | 13 | 21 |
✔. Time , space with two variables.
8.2 Climbing stairs (1 or 2 steps at a time)
.
| 0 | 1 | 2 | 3 | 4 | 5 | |
|---|---|---|---|---|---|---|
| 1 | 1 | 2 | 3 | 5 | 8 |
✔. (If 1,2,3 steps allowed: , tribonacci.)
8.3 Number of paths in an grid (right/down only)
Closed form: . For grid (3 rows, 3 cols): . Table:
| 0 | 1 | 2 | |
|---|---|---|---|
| 0 | 1 | 1 | 1 |
| 1 | 1 | 2 | 3 |
| 2 | 1 | 3 | 6 |
✔ Answer 6.
Matrix Chain Multiplication (full table)
Dimensions: , , . So , matrices.
State: = min scalar multiplications to compute .
Recurrence:
Answer: . Subproblems: ; time: ; number of parenthesizations: Catalan .
Fill by chain length:
| Chain length | Cell | Computation | Value |
|---|---|---|---|
| 1 | base | 0 | |
| 2 | : | 1500 | |
| 2 | : | 9000 | |
| 3 | : | ||
| : | 4500 |
Optimal cost = 4500, split at : . Check: costs giving ; then costs ; total . ✔
Catalan for : parenthesizations: and . ✔
Optimal Binary Search Tree (idea only)
State: = min expected search cost for keys . The added term is the sum of probabilities in the range (every node in the subtree gains one comparison). Time , space . The optimal root is not necessarily the highest-probability key — a common trap.
Memoisation vs Tabulation
| Aspect | Top-down (memoisation) | Bottom-up (tabulation) |
|---|---|---|
| Direction | Recursion + cache | Iterative fill |
| Computes | Only needed states | All states |
| Stack | recursion stack | None |
| Overhead | Function-call overhead | Lower |
| Space optimisation | Hard | Easy (rolling rows) |
| Risk | Stack overflow on deep | Must get fill order right |
Both give the same asymptotic time; memoisation can be faster when few states are reachable.
Problems That Look Like DP but Are Greedy
| Problem | Greedy works? | Why |
|---|---|---|
| Fractional knapsack | Yes (value/weight ratio) | Fractional — exchange argument |
| Activity selection | Yes (earliest finish) | Exchange argument |
| Huffman coding | Yes | Exchange argument (two least frequent are siblings at the deepest level) |
| Dijkstra (non-negative weights) | Yes | Greedy + relaxation |
| 0/1 knapsack | No | Needs DP |
| Coin change (min coins) | No in general (e.g. , ) | Greedy gives 3, optimal 2 |
| LIS | No simple greedy | Needs DP / patience |
| Rod cutting | No | Needs DP |