A Brief Introduction to Dynamic Programming
Dynamic Programming is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of subproblems, so that we do not have to re-compute them when needed later.
example
1 | // recursion |
Two pattern of solving DP problem:
- Tabulation: bottom up
- Memoization: top down
Four steps to solve DP problems
- Determine the meaning of each DP element. It also determines the dimensions, the element type, and the size of the DP cache.
- Initialize DP cache. Initialization is based on a small number of known states, such as the first two Fibonacci numbers.
- Transition. We deduce the values of unknown DP elements from known states. For bottom-up DP, the transition process is usually based on loops. The order of loops depends on the transition equation.
- Return the result. It may require additional traversals on the DP cache.
A Brief Introduction to Dynamic Programming
http://yenotes.org/2021/12/23/A-Brief-Introduction-to-Dynamic-Programming/