First thing how do we verify it's a DP quetion
- Check if you've choice given
- Check if we've to optimize our value
To get started for DP we have two ways:-
- Top Down approach
- Recursive + Memoization
I suggest you to never start with top down approach. Best approach to start is Recursive + Memoization It's easy to think and is powerful as top down approach
So, 1st step make a choice diamgram - which will become easiest part to implement and then think of base case
Base Case - Think of smallest valid input, is a great way to think. Now implement choice diagram
2nd Step is memoization To memoize and program we've to make a table 'T' of dimension m*n. Now, On which size of matrix depends upon?
Check in every fn call which variables are changing. Now initialize complete matrix with -1 Now before calling any function first check whether at T[m][n] is there value present present but not -1. Then simply return that value or else call the funtion and save that returned value to T[m][n].
Variants of Knapsack problem
- Subset sum problem
- Equal sum problem
- Count of subset sum
- Minimum subset sum off
- Target sum
- Number of subset <= given difference