Module 7 · Drill 1 · DP reps

DP classics

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.

1 — Climbing stairs

2 — House robber

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.

3 — Longest common subsequence

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].

4 — Coin change (fewest coins)

Solutions

1 — Climbing stairs

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;
}
  
O(n) time, O(1) space. Same recurrence as Fibonacci. Only the last two cells matter, so two variables suffice — the space-optimized form of bottom-up DP.

2 — House robber

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;
}
  
O(n) time, O(1) space. dp[i] = max(dp[i-1], dp[i-2] + nums[i]) — either skip house i or rob it. Since only the last two values matter, two variables suffice.

3 — Longest common subsequence

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];
}
  
O(n × m) time and space. Can be reduced to O(min(n, m)) space by keeping only two rows. Match → diagonal + 1; mismatch → max of left or up. The classic 2D-DP recurrence underlies edit distance, diff tools, DNA alignment.

4 — Coin change

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;
}
  
O(amount × #coins) time, O(amount) space. dp[a] = min coins to make amount a. For each amount, try every coin; if coin c is usable, the best result via c is dp[a-c] + 1. INF sentinel for unreachable amounts.
Notebook prompt — the ladder

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.