Rough Approach of DP problem types π€
The problem can be of two types:
- like in permutation I can only choose 1 element from 1 row
- or I can choose 1 element 'SEVERAL' no. of times
1.πΆ can only choose 1 element from 1 rowβ
Question might have asked:β
β min or max AMOUNTβ
the
dp(_,_)function returns the AMOUNT
info
This return AMOUNT is determined by the base case like:
if (row == C)
return M - budget; // budget is remaining budget
- need to have a 2D vector/array to store
row& theircol - create
rowas one of the states - for each recursive step make
row+1 - have another state
- then in each row choose the
1element/col you want as per problem & make required changes to the OTHER statealways use
min(_,_,_)ormax(_,_,_)to get OPTIMAL Solution in EACH step
2.πΆ can choose 1 element SEVERAL no. of timesβ
Question might have asked:β
β min or max no. of steps taken to reach Optimal Solutionβ
the
dp()function returns the COUNT of steps
info
This return AMOUNT is determined by the base case like:
if (total == 0) return 0; // here total is total Coins
// as total coins is `0`, so the no. of steps taken is also `0`
- no need to make any
rowstate : as all the elements can be chosen multiple times, no point in creating row - always choose each element
always use
min(_,_,_)ormax(_,_,_)to get OPTIMAL Solution in EACH step - to COUNT steps just add
+1to the thedp(_)function call likedp(_) + 1ans = min(ans, dp(x-c)+1);