Module 10 · Dynamic Programming and Greedy · Lesson 35 of 40

DP patterns: take or skip, grids, knapsack, sequences

The common shapes of DP problems: House Robber, grid paths, 0/1 knapsack, coin change and longest common subsequence.

In this lesson · about 28 minutes

  • Solve 'take it or skip it' decision problems
  • Understand the knapsack and coin-change tables
  • Recognise a DP problem from its wording

Pattern 1: take it or skip it (House Robber)

Houses in a row hold money. You can't take from two houses next to each other. What is the most you can take?

At house i you have two choices: skip it (keep the best up to house i − 1) or take it (its money plus the best up to house i − 2).

best[i] = max(best[i - 1], best[i - 2] + money[i])

Pattern 2: grids

For "paths" or "cheapest path" on a grid moving right/down, each cell depends on the cell above and the cell to the left. Fill row by row: dp[r][c] = grid[r][c] + min(dp[r-1][c], dp[r][c-1]).

Pattern 3: knapsack

A bag holds at most W kilos. Each item has a weight and a value. Pick items (each at most once) to get the most value. This is the 0/1 knapsack.

State: dp[w] = best value with capacity w. For each item, go through capacities from high to low and decide take or skip:

for weight, value in items:
    for w in range(W, weight - 1, -1):
        dp[w] = max(dp[w], dp[w - weight] + value)

Going high-to-low makes sure each item is used only once. Related problems: subset sum (can some items add up to exactly T?) and equal partition. If items can be reused (unbounded knapsack, like coin change), go low-to-high instead.

Pattern 4: two sequences

The longest common subsequence (LCS) of "ABCBDAB" and "BDCABA" is 4 letters long ("BCBA"). A 2D table compares every prefix of one with every prefix of the other: if the letters match, dp[i][j] = dp[i-1][j-1] + 1; otherwise take the better of dropping one letter from either string. Edit distance (fewest changes to turn one word into another) uses the same kind of table.

More patterns you'll meet later

Longest increasing subsequence, interval DP (best way to split a range), DP on trees, bitmask DP (small sets as numbers), stock buying with states (holding / not holding).

Try it: House Robber and knapsack

Exercise

House Robber

Write rob(money) using a table best where best[i] is the most you can take from houses 0 to i. Use best[i] = max(best[i - 1], best[i - 2] + money[i]).

Exercise

Fewest coins

Write fewest_coins(coins, amount) that returns the smallest number of coins that add up to amount (you may reuse each coin), or -1 if it's impossible. Use dp[a] = fewest coins for amount a, starting with dp[0] = 0 and everything else "infinity".

Flashcards

Which DP pattern?

0 of 7 known

Read the problem, guess the pattern, then flip.

7 left in the deck

Check your understanding0 of 2 correct

Question 1

1.In 0/1 knapsack with a 1D table, why loop the capacity from high to low?

Question 2

2.Which words in a problem hint at DP?

Quiz score: 0 of 2

Exercises solved: 0 of 2

Tip: answer every quiz question correctly first.

Noviq Academy is free and for learning. This course follows the topics of a set of DSA study notes, rewritten in Noviq's own words with Python examples. Python runs in your own browser with Pyodide; nothing you type is sent to Noviq.