PATTERN 13: Dynamic Programming (DP)

D

Qubits of DPK

April 11, 2026

Core DSA
Pattern 13 is the boss level — but if you approach it correctly, it becomes systematic, not scary.
Remember this:
DP is not a topic. It’s disciplined recursion.

Core Idea (lock this sentence)

“Avoid solving the same subproblem more than once.”
Every DP problem starts as:
  • Recursion → repeated work → memoization → tabulation → 1D Tabilation if possible
If you skip recursion understanding, DP will feel like memorization.

How to Recognise This Pattern (CRITICAL)

Think DP when:
  • There are overlapping subproblems
  • You’re asked for maximum / minimum / count
  • A greedy choice fails
  • Constraints hint O(N²) → O(N)
If recursion tree repeats → DP.

30 MUST-DO INTERVIEW PROBLEMS

(10 Easy → 10 Medium → 10 Hard)

EASY : DP Basics (1D)

Goal: understand state definition.
  1. #
    Tribonacci Number – LC 1137
  2. #
    House Robber – LC 198
  3. #
    House Robber II – LC 213
  4. #
    Min Cost Climbing Stairs – LC 746
  5. #
    Decode Ways – LC 91
  6. #
    Counting Bits – LC 338
  7. #
    Divisor Game – LC 1025
  8. #
    Frog Jump (basic DP) – GFG
  9. #
    Jump Game (DP view) – LC 55
  10. #
    Pascal's Triangle – LC 118
After these:
You should define dp[i] clearly without guessing.

️ MEDIUM : 2D DP & Choice-Based States

Write state + transition in English first.
  1. #
    Unique Paths – LC 62
  2. #
    Unique Paths II – LC 63
  3. #
    Minimum Path Sum – LC 64
  4. #
    Coin Change – LC 322
  5. #
    Coin Change II – LC 518
  6. #
    Partition Equal Subset Sum – LC 416
  7. #
    Longest Increasing Subsequence – LC 300
  8. #
    Longest Common Subsequence – LC 1143
  9. #
    Edit Distance – LC 72
  10. #
    Longest Palindromic Subsequence – LC 516
Interviewers check:
Can you explain state + transition

HARD : DP Mastery Problems

These test pattern recognition inside DP.
  1. #
    Target Sum – LC 494
  2. #
    Word Break – LC 139
  3. #
    0/1 Knapsack – GFG
  4. #
    Unbounded Knapsack – GFG
  5. #
    Matrix Chain Multiplication – GFG
  6. #
    Regular Expression Matching – LC 10
  7. #
    Wildcard Matching – LC 44
  8. #
    Distinct Subsequences – LC 115
  9. #
    Maximum Alternating Subsequence Sum – LC 1911
  10. #
    Interleaving String – LC 97
These require:
  • Clean state modeling
  • Space optimization awareness
  • Zero memorization

DP SOLVING CHECKLIST (INTERVIEW GOLD)

Before coding, always say:
  1. #
    What is my state?
  2. #
    What are my choices?
  3. #
    Base case?
  4. #
    Transition?
  5. #
    Final answer location?
If you answer these → you win.

Common Interview Killers

  • Writing DP without explaining recursion
  • Wrong state definition
  • Mixing greedy logic
  • Memorizing solutions

INTERVIEW REVISION CARD — Pattern 13

1️⃣ Pattern in One Line

"Avoid solving the same subproblem twice — disciplined recursion with memory."

2️⃣ Recognize It When...

  • Overlapping subproblems in recursion tree
  • Asked for maximum/minimum/count/ways
  • Greedy fails — local choice isn't always globally optimal
  • Trigger words: "number of ways", "minimum cost", "maximum profit", "can you achieve"

3️⃣ Don't Confuse With...

  • Greedy (P9) → greedy commits once; DP explores all choices
  • Backtracking (P10) → backtracking generates all; DP counts/optimizes
  • Divide & Conquer → D&C subproblems don't overlap; DP subproblems do

4️⃣ Approach in 3 Steps

  1. #
    Define state: dp[i] means "best answer for subproblem i"
  2. #
    Write transition: dp[i] = f(dp[i-1], dp[i-2], ...)
  3. #
    Follow strict order: Recursion → Memoization → Tabulation → Space Optimization

5️⃣ Invariant to Maintain

"dp[i] always contains the correct optimal answer for the subproblem of size i, regardless of how it was computed."

6️⃣ Complexity to Justify

  • Time: O(N) or O(N²) — number of unique states × transition cost
  • Space: O(N) or O(1) after space optimization
  • Why: Each subproblem solved exactly once

7️⃣ Edge Cases to Always Check

  • Base cases: dp[0], dp[1] defined correctly
  • Empty input — return 0 or base value
  • Negative numbers affecting transitions
  • Single element array
  • K = 0 or target = 0

8️⃣ One Line to Say in Interview

"This has overlapping subproblems, so I'll use DP — define state, write transition, then optimize space if possible."