Skip to content

Chapter 5 of 7

Dynamic Programming

Dynamic Programming solves problems by breaking them into overlapping subproblems and storing results to avoid recomputation. The Fibonacci example illustrates both approaches: top-down memoization uses recursion with a cache, while bottom-up tabulation iteratively builds the solution. Bottom-up often achieves O(1) space when only the previous values are needed, as in House Robber and Climbing Stairs problems where the recurrence simplifies to Fibonacci-like formulas.

Classic DP problems include the 0/1 Knapsack (each item used at most once), Unbounded Knapsack (items reusable), Coin Change (minimum coins or count ways), and Longest Common Subsequence. These problems typically use 1D or 2D DP arrays with transitions based on include/exclude choices. The target sum and partition equal subset sum problems reduce cleverly to knapsack variants by transforming the problem formulation.

String-based DP problems form a substantial category. Edit Distance (Levenshtein) computes minimum insertions, deletions, and substitutions using a 2D table. The Longest Palindromic Subsequence equals the LCS of a string and its reverse. Regular Expression Matching and Wildcard Matching handle pattern matching with special characters. Interval DP applies to problems where decisions affect ranges, like Burst Balloons and Matrix Chain Multiplication. The Super Egg Drop problem inverts the DP direction, computing the maximum floors checkable with given moves and eggs. DP on DAGs and trees extends the technique to graph structures, with digit DP handling counting problems with positional constraints.

All chapters
  1. 1Searching and Sorting Foundations
  2. 2Array Techniques and Problem Patterns
  3. 3Trees and Hierarchical Structures
  4. 4Graph Algorithms
  5. 5Dynamic Programming
  6. 6Advanced Data Structures
  7. 7String Algorithms and Specialized Techniques

Drill it

Reading is not remembering. These come from the Algorithms Code deck:

Q

What is Binary Search and what is its time complexity?<br><br>Write the implementation in Python.

Binary Search finds a target in a sorted array by repeatedly halving the search space.Time: O(log n) &nbsp;|&nbsp; Space: O(1)def binary_search(arr, target):...

Q

What is Bubble Sort and what is its time complexity?<br><br>Write the implementation in Python.

Bubble Sort repeatedly swaps adjacent elements if they are in the wrong order.Time: O(n²) &nbsp;|&nbsp; Space: O(1)def bubble_sort(arr): n = len(arr) for...

Q

What is Merge Sort and what is its time complexity?<br><br>Write the implementation in Python.

Merge Sort is a divide-and-conquer algorithm that splits the array in half, recursively sorts each half, then merges them.Time: O(n log n) &nbsp;|&nbsp; Space:...

Q

What is Quick Sort and what is its time complexity?<br><br>Write the implementation in Python.

Quick Sort picks a pivot, partitions elements around it, then recursively sorts each partition.Time: O(n log n) avg, O(n²) worst &nbsp;|&nbsp; Space: O(log n)de...