Back to DSA DashboardConcept Mastery
DSA Core Concept

Dynamic Programming

Dynamic Programming (DP) is an optimization technique that solves complex problems by breaking them down into overlapping subproblems. It solves each subproblem once and caches the result (memoization or tabulation) to prevent redundant recalculations.

Why It Exists

Standard recursion can calculate identical subproblems repeatedly, resulting in exponential O(2^N) runtimes. DP caches duplicate calculations, reducing runtimes to linear or portfolio scales.

Intuition for Beginners

If I ask you what 1 + 1 + 1 + 1 + 1 is, you count and say 5. If I add another '+ 1' to the end, you don't recount from the start; you remember 5 and add 1 to get 6. Remembering past results is the core of DP.

Visual Explanation

Recursive duplicate tree vs. DP linear memoization:
      Fib(4)                    Fib(4) [Calculates once]
     /      \                  /      \
  Fib(3)    Fib(2)          Fib(3)    Fib(2) [Fetched from cache]
  /    \                     /
Fib(2) Fib(1)             Fib(2)

Complexity Analysis

ApproachTime ComplexitySpace ComplexityNotes
Naive RecursionO(2^N)O(N)Duplicate sub-calls grow exponentially
MemoizationO(N)O(N) + O(N)Top-down: recursion stack + cache hash
TabulationO(N)O(N)Bottom-up: iterative array table
Space OptimizedO(N)O(1)Maintain only last two states

Production & Real World Applications

  • Git Diff: Longest Common Subsequence (LCS) algorithm comparing text changes.
  • Routing Routers: Computing shortest path transitions dynamically.

Algorithmic Patterns

Memoization (Top-Down)

Adding cache registers inside recursive functions to intercept duplicate calculations.

Tabulation (Bottom-Up)

Filling an array iteratively from base cases up to target index limits.

Defending the Design (Interview Q&A)

Q:How do you identify if a problem should be solved using Dynamic Programming?
A problem is a DP candidate if it exhibits two properties: 1. Overlapping Subproblems (computations repeat in recursion) and 2. Optimal Substructure (the global optimal solution can be built from optimal solutions of subproblems).

Common Traps & Mistakes

  • ⚠️Not identifying the correct base cases.
  • ⚠️Overallocating table space when only the last few indices are needed (wastes memory).

Practice Problems Mapping

Climbing StairsSolve
Coin ChangeSolve
Longest Common SubsequenceSolve