Module 7 · Drill 1 · DP reps
Four problems that walk you up the four-stage ladder. For each, write the recursive version first, then memoize, then (if you have time) tabulate.
You're robbing a row of houses, each with money. You cannot rob two adjacent houses. Return the max money. State: dp[i] = max if you consider the first i houses.
Two strings. Find the length of the longest sequence appearing in both, in order, not necessarily contiguous. State: dp[i][j] = LCS length of a[:i] and b[:j].
def climb_stairs(n):
if n <= 1: return 1
a, b = 1, 1
for _ in range(n - 1):
a, b = b, a + b
return b
function climbStairs(n) {
if (n <= 1) return 1;
let a = 1, b = 1;
for (let i = 0; i < n - 1; i++) {
[a, b] = [b, a + b];
}
return b;
}
function climbStairs(n: number): number {
if (n <= 1) return 1;
let a = 1, b = 1;
for (let i = 0; i < n - 1; i++) {
[a, b] = [b, a + b];
}
return b;
}
def rob(nums):
prev, curr = 0, 0
for x in nums:
prev, curr = curr, max(curr, prev + x)
return curr
function rob(nums) {
let prev = 0, curr = 0;
for (const x of nums) {
[prev, curr] = [curr, Math.max(curr, prev + x)];
}
return curr;
}
function rob(nums: number[]): number {
let prev = 0, curr = 0;
for (const x of nums) {
[prev, curr] = [curr, Math.max(curr, prev + x)];
}
return curr;
}
def lcs(a, b):
n, m = len(a), len(b)
dp = [[0] * (m + 1) for _ in range(n + 1)]
for i in range(1, n + 1):
for j in range(1, m + 1):
if a[i-1] == b[j-1]:
dp[i][j] = dp[i-1][j-1] + 1
else:
dp[i][j] = max(dp[i-1][j], dp[i][j-1])
return dp[n][m]
function lcs(a, b) {
const n = a.length, m = b.length;
const dp = Array.from({ length: n + 1 }, () => new Array(m + 1).fill(0));
for (let i = 1; i <= n; i++) {
for (let j = 1; j <= m; j++) {
if (a[i-1] === b[j-1]) dp[i][j] = dp[i-1][j-1] + 1;
else dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]);
}
}
return dp[n][m];
}
function lcs(a: string, b: string): number {
const n = a.length, m = b.length;
const dp: number[][] = Array.from({ length: n + 1 }, () => new Array(m + 1).fill(0));
for (let i = 1; i <= n; i++) {
for (let j = 1; j <= m; j++) {
if (a[i-1] === b[j-1]) dp[i][j] = dp[i-1][j-1] + 1;
else dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]);
}
}
return dp[n][m];
}
def coin_change(coins, amount):
INF = amount + 1
dp = [0] + [INF] * amount
for a in range(1, amount + 1):
for c in coins:
if c <= a:
dp[a] = min(dp[a], dp[a - c] + 1)
return dp[amount] if dp[amount] < INF else -1
function coinChange(coins, amount) {
const INF = amount + 1;
const dp = new Array(amount + 1).fill(INF);
dp[0] = 0;
for (let a = 1; a <= amount; a++) {
for (const c of coins) {
if (c <= a) dp[a] = Math.min(dp[a], dp[a - c] + 1);
}
}
return dp[amount] < INF ? dp[amount] : -1;
}
function coinChange(coins: number[], amount: number): number {
const INF = amount + 1;
const dp = new Array(amount + 1).fill(INF);
dp[0] = 0;
for (let a = 1; a <= amount; a++) {
for (const c of coins) {
if (c <= a) dp[a] = Math.min(dp[a], dp[a - c] + 1);
}
}
return dp[amount] < INF ? dp[amount] : -1;
}
For each of the four problems, write the four-stage progression: "Recurse → memoize → tabulate → space-optimize." If you can articulate where on this ladder you are while solving live, you'll never be stuck on a DP question again.