<aside>

This section is part of the 5-hour LeetCode DSA Foundations course by Mukul Raina

C11: DSA Foundations for Interviews

Section 17: Dynamic Programming Foundations


SLIDE 162: Section Intro, Dynamic Programming

Section 17: Dynamic Programming Foundations

Dynamic programming is the technique freshers fear most, but the core concept is simple: recursion with memory. If a recursive solution solves the same subproblem multiple times, store each result the first time and look it up every time after. That single optimization takes exponential algorithms to polynomial.

Section 12 built recursive thinking: base case, recursive case, trust the recursion. Section 13 introduced greedy algorithms and showed where they fail: coin change with denominations [1, 3, 4] and target 6. Greedy picked 4+1+1 = 3 coins. The optimal answer is 3+3 = 2 coins. Section 13 ended with the statement: "When greedy fails and the problem has overlapping subproblems, reach for DP." This section delivers on that promise.

This is foundations-level DP: what DP is, how to convert recursion into DP, the three-part recipe that applies to every DP problem, and four classic problems that appear repeatedly in fresher interviews. Advanced DP patterns (interval DP, DP on trees, bitmask DP, multi-dimensional state design) are covered in C6.

What We'll Cover:

  1. When recursion becomes DP: overlapping subproblems and the memoization fix
  2. Top-down (memoization) vs bottom-up (tabulation): two routes to the same answer
  3. The DP recipe: state definition, transition recurrence, base cases
  4. Four worked problems: Climbing Stairs, House Robber, Coin Change, Longest Increasing Subsequence
  5. Space optimization: reducing O(n) to O(1) when only recent values are needed

Where DP Connects to Other Sections:

Connection Section How It Applies
Recursion is the starting point for every DP solution Section 12: Recursion DP = recursion + cache for overlapping subproblems
Greedy fails when local choices are not globally optimal Section 13: Greedy DP considers all choices, greedy considers one
Fibonacci memoization was previewed Section 12: Slide 113 Same technique, now formalized and applied to new problems
Coin change greedy failure was previewed Section 13: Slide 122 DP solves it correctly where greedy gave the wrong answer
Subsequence problems from Strings section Section 4: Slide 39 "Subsequence" problems need DP, not substring/sliding window
LIS uses binary search for the O(n log n) optimization Section 8: Binary Search Patience sorting variant covered in C6

<aside>

Mukul Raina | youtube.com/@mukulraina | slides.mukulraina.com | LinkedIn

</aside>