sagelearnai

Dynamic Programming (2-D)

Signal: two sequences to align or compare (two strings), a grid you move through, or a 1-D problem that needs a second dimension (index × remaining capacity, interval [i, j]).

Core idea: dp[i][j] depends on a few neighboring cells: typically up dp[i−1][j], left dp[i][j−1] and diagonal dp[i−1][j−1]. Fill the table in an order where those are ready (usually row by row).

The classic string DP

For strings a (length m) and b (length n), let dp[i][j] describe the prefixes a[:i] and b[:j], with an extra row and column of base cases for empty prefixes:

ProblemIf a[i−1] == b[j−1]ElseBase
Longest Common Subsequencedp[i−1][j−1] + 1max(dp[i−1][j], dp[i][j−1])0
Edit Distancedp[i−1][j−1]1 + min(delete, insert, replace)dp[i][0] = i, dp[0][j] = j

Space optimization: if row i only needs row i−1, keep two rows (or one row plus a saved diagonal value): O(n) instead of O(m·n).

Interval DP

dp[i][j] = answer for the substring/subarray i..j, filled by increasing length, e.g., palindromic substrings, burst balloons, matrix-chain multiplication. Palindromic Substrings (in 1-D DP) can be solved this way, or with an O(1)-space "expand around center" trick.

Problems

  1. Unique Paths (Medium): dp[r][c] = dp[r−1][c] + dp[r][c−1]
  2. Unique Paths II (Medium): dp[c] += dp[c−1] row by row; an obstacle zeroes the cell
  3. Minimum Path Sum (Medium): dp[r][c] = grid[r][c] + min(top, left)
  4. Longest Common Subsequence (Medium): Match → diagonal + 1; else max(up, left)
  5. Longest Palindromic Subsequence (Medium): Interval DP: dp[i][j] = dp[i+1][j−1]+2 if s[i]==s[j] else max(dp[i+1][j], dp[i][j−1])
  6. Best Time to Buy and Sell Stock with Cooldown (Medium): Three states: hold, sold (cooldown next), rest
  7. Target Sum (Medium): P − N = target and P + N = sum ⇒ count subsets with sum (sum+target)/2
  8. Interleaving String (Medium): dp[i][j] = can s1[:i] and s2[:j] form s3[:i+j]
  9. Maximal Square (Medium): dp[r][c] = 1 + min(top, left, top-left) for '1' cells
  10. Edit Distance (Medium): Levenshtein: min(delete, insert, replace) + 1
  11. Distinct Subsequences (Hard): dp[j] += dp[j−1] when s[i]==t[j−1], iterating j backward
  12. Burst Balloons (Hard): Interval DP choosing the LAST balloon burst in (i, j)
  13. Regular Expression Matching (Hard): dp[i][j]: '*' means zero copies (j−2) or one more copy if p[j−2] matches s[i−1]
  14. Triangle (Medium): Bottom-up: dp[j] = row[j] + min(dp[j], dp[j+1])
  15. Minimum Falling Path Sum (Medium): dp[r][c] = m[r][c] + min of three cells above
  16. Ones and Zeroes (Medium): 0/1 knapsack with two capacities; iterate both downward
  17. Longest String Chain (Medium): Sort by length; dp[w] = 1 + max dp[w with one char removed]
  18. Count Square Submatrices with All Ones (Medium): Same dp as Maximal Square; sum all dp values
  19. Dungeon Game (Hard): Go backwards from the princess: need[r][c] = max(1, min(right, down) − cell)
  20. Cherry Pickup (Hard): Two walkers go down-right together; state (step, r1, r2); count a shared cell once
  21. Wildcard Matching (Hard): dp: '*' = dp[i][j−1] (empty) or dp[i−1][j] (eat one more char)
  22. Maximum Length of Repeated Subarray (Medium): dp[i][j] = dp[i−1][j−1] + 1 if equal else 0 (longest common substring)
  23. Pascal's Triangle (Easy): Each inner entry is the sum of the two entries above it