Web Analytics

๐Ÿ“Š DP Problems: 1D, 2D & Knapsack

Intermediate to Advanced ~45 min read

Now that you understand DP fundamentals, let's solve real problems! We'll explore 1D DP (single array), 2D DP (matrix/grid), and Knapsack problems - three essential DP patterns used in countless interviews and real-world applications.

Part 1: 1D DP Problems

1D DP uses a single array where dp[i] represents the answer for subproblem ending at index i.

House Robber Problem

Problem

You can't rob adjacent houses. Maximize total money robbed.

State: dp[i] = maximum money robbed up to house i

Transition: dp[i] = max(rob house i, skip house i) = max(dp[i-2] + houses[i], dp[i-1])

Coin Change

Two Variants

Minimum Coins: Fewest coins to make amount

Number of Ways: Total ways to make amount

Key insight: For each amount, try all coin choices

Other 1D DP Problems

  • Decode Ways: Ways to decode string (A=1, B=2, ..., Z=26)
  • Longest Increasing Subsequence (LIS): Length of longest increasing subsequence
  • Palindrome Partitioning: Minimum cuts to partition into palindromes

The Complete Code: 1D DP

Output
Click Run to execute your code

Part 2: 2D DP Problems

2D DP uses a matrix where dp[i][j] represents the answer at position (i, j) or for subproblem involving first i elements of sequence 1 and first j elements of sequence 2.

Longest Common Subsequence (LCS)

Problem

Find longest subsequence common to both strings.

State: dp[i][j] = LCS length of text1[0:i] and text2[0:j]

Transition: If match: dp[i][j] = dp[i-1][j-1] + 1, else: dp[i][j] = max(dp[i-1][j], dp[i][j-1])

Edit Distance (Levenshtein)

Problem

Minimum operations (insert, delete, replace) to convert word1 to word2.

Application: Spell checkers, DNA sequence alignment, fuzzy matching

Unique Paths

Robot can move right or down. How many paths from top-left to bottom-right?

Transition: dp[i][j] = dp[i-1][j] + dp[i][j-1]

The Complete Code: 2D DP

Output
Click Run to execute your code

Part 3: Knapsack Problems

Knapsack problems involve selecting items with constraints (weight, value, capacity).

0/1 Knapsack

Problem

Each item can be taken at most once. Maximize value within weight capacity.

State: dp[i][w] = maximum value using first i items with capacity w

Transition: dp[i][w] = max(don't take: dp[i-1][w], take: dp[i-1][w-weight[i]] + value[i])

Unbounded Knapsack

Key Difference

Items can be taken unlimited times. Same as coin change problem!

Transition: dp[w] = max(dp[w-weight[i]] + value[i]) for all items

Subset Sum

Can we form target sum using subset of numbers? Boolean DP problem.

The Complete Code: Knapsack

Output
Click Run to execute your code

Pattern Summary

Key Patterns

  • 1D DP: dp[i] = answer ending/up to index i. Common: max/min, sum, count
  • 2D DP: dp[i][j] = answer at (i,j) or for sequences up to i and j. Common: paths, matching, optimization
  • Knapsack: dp[i][w] = best value with capacity w. Common: include/exclude choices

Complexity Analysis

Problem Type Time Space
1D DP O(n) O(n) or O(1)
2D DP O(m ร— n) O(m ร— n) or O(n)
Knapsack O(n ร— W) O(W)

Summary

What You've Learned

  1. 1D DP: Single array, linear problems (House Robber, Coin Change, LIS)
  2. 2D DP: Matrix, grid/sequence problems (LCS, Edit Distance, Unique Paths)
  3. Knapsack: Selection with constraints (0/1, Unbounded, Subset Sum)
  4. Space Optimization: Often can reduce from O(nยฒ) to O(n) or O(1)
  5. Pattern Recognition: Learn to identify which DP pattern fits a problem

What's Next?

You've mastered the core DP patterns! Next, we'll explore Advanced DP - DP on strings, trees, bitmask DP, and problem-solving strategies to tackle any DP challenge!