0

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.

  1. Try out all possible combinations of size r ( r=1 to n ) and see which one works.
  2. 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?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • If you can make up any amount of change, then there must be a unit coin (1 cent, 1 penny, …). And the minimum number of denominations is then 1 — the change is given only in the unit currency. So, the algorithm is probably (or, at least, may be) correct. If you can't necessarily give change for any arbitrary amount, then you may have a more interesting exercise. If you only have a limited number of any given denomination, you also have a more interesting exercise. – Jonathan Leffler Apr 11 '20 at 14:54
  • Yeah supposing I don't have have unit denomination and the no. of coins are limited. How would I solve the problem? – hello-fri-end Apr 12 '20 at 01:27

0 Answers0