Overview
Dynamic programming in DSA is a technique where you break a problem into smaller overlapping subproblems, solve each subproblem once, and reuse those results instead of recalculating them.Two Main DP Styles
Top-down (memoization): Solve the problem recursively and store the results of solved subproblems to avoid redundant calculations. Bottom-up (tabulation): Solve smaller subproblems first, store results in a table, and use them to build solutions to larger problems.Examples
Fibonacci
- Top-down - Time:
O(n), Space:O(n) - Bottom-up - Time:
O(n), Space:O(1)