DSA: 1D Dynamic Programming, Memoization & Tabulation
When to use
Use this skill when tackling classic interview problems like Climbing Stairs, Coin Change, House Robber, Decode Ways, or Longest Increasing Subsequence.
Core principles
- Optimal Substructure: The optimal solution to the problem can be constructed from optimal solutions of its sub-problems.
- Overlapping Sub-problems: Recursive decomposition encounters the same sub-problems repeatedly, allowing caching (memoization) or bottom-up calculation (tabulation).
- State Definition Clarity: Define precisely what
dp[i]represents before writing code (e.g., 'minimum coins to make amounti'). - Space Optimization Rolling Variables: If
dp[i]depends only ondp[i-1]anddp[i-2], eliminate the O(N) array in favor of two variables for O(1) space.
Step-by-step workflow
- Define the recurrence relation and exact state meaning of
dp[i]. - Identify base cases (e.g.,
dp[0] = 0ordp[0] = 1). - Choose approach: top-down with memoization (
@functools.lru_cache) or bottom-up tabulation. - Fill table iteratively from smallest sub-problems to target
N. - Apply space optimization if previous states allow rolling updates.
- Trace a small test case (e.g.,
amount = 5) manually out loud.
Specifics & examples
# Coin Change (Fewest coins to make amount): O(amount * len(coins)) Time
def coin_change(coins: list[int], amount: int) -> int:
dp = [float('inf')] * (amount + 1)
dp[0] = 0
for i in range(1, amount + 1):
for coin in coins:
if i - coin >= 0:
dp[i] = min(dp[i], dp[i - coin] + 1)
return dp[amount] if dp[amount] != float('inf') else -1
Common mistakes & pitfalls
- Failing to initialize DP array with appropriate identity values (
inffor min,-inffor max). - Off-by-one errors in allocating
dptable size (amountvsamount + 1). - Struggling to communicate the state transition equation to the interviewer before coding.
Verification checklist
- State definition
dp[i]is explicitly stated to the interviewer. - Handled impossible targets (returning -1).
- Analyzed space optimization from O(N) to O(1) when applicable.
Related skills
- dsa-dynamic-programming-2d-knapsack-lcs
- dsa-greedy-algorithms-interval-scheduling
- dsa-time-space-complexity-big-o-analysis
- coding-interview-communication-thinking-out-loud