Variation 1:
What if in coin change problem, you had to find out the minimum number of denominations that can be used to give change for an input X. I understand the Greedy & DP approach for this.
- Try out all possible combinations of size r ( r=1 to n ) and see which one works.
- Create a matrix of dimensions (x+1) x n and use DP.
I was thinking of a recursive solution where for each denomination d[i]
, I'll recursively call min_cur function when d[i]
is excluded and when at least of d[i]
is included and pick the minimum out of two.
for all i range of d[0] to d[n]
min_cur( x -> (d[1], d[2], …, d[n]) ) =
minimum( min_cur( x -> (d[1], d[2], …, d[n] excluding d[i]) ),
min_cur( (x-i) -> (d[1], d[2], …, d[n]) ).
but this recursion always gives the answer as 1.
It needs some refinement. I'm not sure how to refine it. Also, what if you also wanted to print all the denominations used?
Variation 2:
Same problem as above but with a limited number of coins for each denomination — i.e for d[i]
, you can at most use l[i]
coins. Does this have optimal substructure? I'm not sure since the subproblems are dependent on one another. What would be the best greedy approach for this?