DP Notes
The layman's way of analyzing the runtime of a memoized recursive function is :
Work per subproblem * Number of subproblems
Recursive -> This approach is called top-down, as we can call the function with a query value and the calculation starts going from the top (queried value) down to the bottom (base cases of the recursion), and makes shortcuts via memorization on the way.
Iterative -> This approach is called bottom-up, Bottom up is exactly the opposite of top-down, you start at the bottom (base cases of the recursion), and extend it to more and more values.
How to think in iterative DP?
1. Define what dp[i][j] or dp[i] will represent in your problem.
2. Initialize the base cases of the dp array.
KnapSack Problem:
Coin Change Problem:
Steps to write DP solution in interview
Explain what DP represent
write how DP is releated to previous DP and write it in comment
Think of base case and when it is needed and write it before iteration
Last updated