Authored by Tony Feng
Created on April 10th, 2022
Last Modified on April 10th, 2022
Understanding Dynamic Programming
Intro
- Dynamic Programming helps to efficiently solve a class of problems that have overlapping subproblems and optimal substructure property.
- Key elements
- Initialization
- State-transition Equation
- Termination
Characteristics
- Overlapping Subproblems
- Any problem has overlapping sub-problems if finding its solution involves solving the same subproblem multiple times.
- Optimal Substructure Property
- Any problem has optimal substructure property if its overall optimal solution can be constructed from the optimal solutions of its subproblems.
Dynamic Programming Methods
- Top-down with Memoization
- In this approach, we try to solve the bigger problem by recursively finding the solution to smaller sub-problems.
- Whenever we solve a sub-problem, we cache its result so that we can just return the saved result if it’s called multiple times.
- Bottom-up with Tabulation
- Tabulation is the opposite of the top-down approach and avoids recursion.
- In this approach, we solve the problem “bottom-up” (i.e. by solving all the related sub-problems first). This is typically done by filling up an n-dimensional table. Based on the results in the table, the solution to the top/original problem is then computed.
Examples
Leetcode 509 Fibonacci Number
|
|
Leetcode 62 Unique Paths
|
|
Reference
- 手把手带你刷Leetcode力扣 - 动态规划 Dynamic Programming
- 手把手带你刷Leetcode力扣 - Leetcode 509
- 手把手带你刷Leetcode力扣 - Leetcode 62
- What is Dynamic Programming?
- Programiz - Dynamic Programming