CampusFlow
AlgorithmsDynamic Programming

🧩 Dynamic Programming

Solve complex problems by breaking them into overlapping subproblems. Master memoization, tabulation, and state transition design.

Overview

ℹ️

What is Dynamic Programming?

DP is an optimization technique that solves problems by combining solutions to overlapping subproblems. It's used when a problem has: 1) Optimal substructure - solution depends on optimal solutions to subproblems, 2) Overlapping subproblems - same subproblems recur multiple times.

Key DP Algorithms

Fibonacci Sequence

Classic problem: compute nth Fibonacci number with memoization

Time: O(n)Space: O(n)

Coin Change Problem

Minimum coins needed to make a target amount

Time: O(n*amount)Space: O(amount)

Longest Common Subsequence

Find longest subsequence common to two strings

Time: O(n*m)Space: O(n*m)

0/1 Knapsack

Maximize value with weight constraint

Time: O(n*W)Space: O(W)

Edit Distance

Minimum operations to convert one string to another

Time: O(n*m)Space: O(n*m)

Interactive Visualizer

DP Table Visualization

🧩

Fibonacci Sequence DP Table Visualizer

Watch how DP tables are filled row by row during the tabulation process.

Deep Dive: Fibonacci Sequence

Time Complexity

Time: O(n)

Space Complexity

Space: O(n)

Approach

Top-down + Bottom-up
💡

DP Pattern

Fibonacci shows the essence of DP: memoizing recursive calls eliminates O(2ⁿ) redundant work, reducing it to O(n). The tabulation approach builds from base cases up.

python

# Memoization approach
def fib(n, memo = {}):
    if n in memo:
        return memo[n]
    if n <= 1:
        return n
    memo[n] = fib(n - 1, memo) + fib(n - 2, memo)
    return memo[n]

# Tabulation approach
def fibTab(n):
    dp = [0] * (n + 1)
    dp[1] = 1
    for i in range(2, n + 1):
        dp[i] = dp[i - 1] + dp[i - 2]
    return dp[n]
1

Define State

What does dp[i] or dp[i][j] represent? This is the most important step.

2

Base Cases

Initialize dp[0], dp[0][0], etc. with known starting values.

3

State Transition

How does dp[i] relate to previous states? This is the recurrence relation.

4

Return Result

The answer is usually dp[n], dp[m][n], or the best value in the table.

Common DP Problems

Climbing Stairs

Easy

dp[i] = dp[i-1] + dp[i-2]

House Robber

Medium

Max of adjacent or current + skip

Longest Increasing Subsequence

Medium

dp[i] = 1 + max(dp[j]) for j < i

Palindrome Substring

Medium

Expand center or DP table

Word Break

Medium

dp[i] = can segment s[0:i]

Maximum Subarray

Easy

Kadane's algorithm: dp[i] = max(arr[i], dp[i-1] + arr[i])

Interview Questions

What's the difference between memoization and tabulation?

Memoization (top-down) uses recursion + cache. Easier to code from recurrence relation but may hit recursion limits. Tabulation (bottom-up) is iterative, often more efficient in space, and avoids recursion overhead. Both have same time complexity.

How do you identify if DP can be applied?

Look for: 1) Problem asks for optimum (min/max/longest/shortest), 2) Decisions affect future outcomes, 3) Overlapping subproblems - brute force recalculates same subproblems. Classic examples: Fibonacci, knapsack, LCS.

How do you optimize DP space?

Check if dp[i] depends only on dp[i-1] or few previous states. You can use 1D array instead of 2D (e.g., knapsack), or just a few variables (e.g., Fibonacci: just 2 vars). This is called 'rolling array' optimization.

Common Mistakes to Avoid

⚠️

Starting with tabulation before understanding the recursive solution

⚠️

Not defining base cases properly - the foundation of correct DP

⚠️

Using too large a state space - carefully choose what to memoize

⚠️

Forgetting to handle edge cases: empty input, single element, zero amount

⚠️

Confusing top-down (memoization) with bottom-up (tabulation) approach

What's Next?

Advanced DP Topics

Level up with these advanced DP patterns: