Dynamic Programming Basics

Dynamic Programming Overview

Dynamic Programming (DP) is a method for solving complex problems by breaking them down into simpler subproblems. It stores the results of subproblems to avoid redundant calculations.

When to Use DP

  • Optimal Substructure: Optimal solution can be constructed from optimal solutions of subproblems
  • Overlapping Subproblems: Same subproblems are solved multiple times
  • Memoization: Store results of expensive function calls
  • Tabulation: Build solution bottom-up using a table

Fibonacci Sequence - Classic DP Example

// Recursive (exponential time)
public int fibRecursive(int n) {
    if (n <= 1) return n;
    return fibRecursive(n - 1) + fibRecursive(n - 2);
}

// Memoization (top-down)
public int fibMemo(int n) {
    int[] memo = new int[n + 1];
    Arrays.fill(memo, -1);
    return fibMemoHelper(n, memo);
}

private int fibMemoHelper(int n, int[] memo) {
    if (n <= 1) return n;
    if (memo[n] != -1) return memo[n];
    
    memo[n] = fibMemoHelper(n - 1, memo) + fibMemoHelper(n - 2, memo);
    return memo[n];
}

// Tabulation (bottom-up)
public int fibTabulation(int n) {
    if (n <= 1) return n;
    
    int[] dp = new int[n + 1];
    dp[0] = 0;
    dp[1] = 1;
    
    for (int i = 2; i <= n; i++) {
        dp[i] = dp[i - 1] + dp[i - 2];
    }
    
    return dp[n];
}

// Space optimized
public int fibOptimized(int n) {
    if (n <= 1) return n;
    
    int prev = 0, curr = 1;
    for (int i = 2; i <= n; i++) {
        int next = prev + curr;
        prev = curr;
        curr = next;
    }
    
    return curr;
}

Climbing Stairs Problem

// Problem: Climb n stairs, can take 1 or 2 steps at a time
public int climbStairs(int n) {
    if (n <= 2) return n;
    
    int[] dp = new int[n + 1];
    dp[1] = 1;
    dp[2] = 2;
    
    for (int i = 3; i <= n; i++) {
        dp[i] = dp[i - 1] + dp[i - 2];
    }
    
    return dp[n];
}

// Space optimized version
public int climbStairsOptimized(int n) {
    if (n <= 2) return n;
    
    int prev = 1, curr = 2;
    for (int i = 3; i <= n; i++) {
        int next = prev + curr;
        prev = curr;
        curr = next;
    }
    
    return curr;
}

House Robber Problem

// Problem: Rob houses, can't rob adjacent houses
public int rob(int[] nums) {
    if (nums.length == 0) return 0;
    if (nums.length == 1) return nums[0];
    
    int[] dp = new int[nums.length];
    dp[0] = nums[0];
    dp[1] = Math.max(nums[0], nums[1]);
    
    for (int i = 2; i < nums.length; i++) {
        dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]);
    }
    
    return dp[nums.length - 1];
}

// Space optimized version
public int robOptimized(int[] nums) {
    if (nums.length == 0) return 0;
    if (nums.length == 1) return nums[0];
    
    int prev = nums[0];
    int curr = Math.max(nums[0], nums[1]);
    
    for (int i = 2; i < nums.length; i++) {
        int next = Math.max(curr, prev + nums[i]);
        prev = curr;
        curr = next;
    }
    
    return curr;
}

DP Problem Categories

  • 1D DP: Fibonacci, climbing stairs, house robber
  • 2D DP: Longest common subsequence, edit distance
  • Knapsack: 0/1 knapsack, unbounded knapsack
  • Matrix DP: Unique paths, minimum path sum
  • String DP: Palindrome, string matching

Open full interactive app