2

We usually the following recurrence relation for the coin change problem: (P is the total money for which we need change and d_i is the coin available)

But can't we make it like this: (V is the given sorted set of coins available, i and j are its subscripts with Vj being the highest value coin given)

C[p,Vi,j] = C[p,Vi,j-1]     if Vj > p
          = C[p-Vj,Vi,j] + 1  if Vj <=p

Is there anything wrong with what I wrote? Though the solution is not dynamic but isn't it more efficient?

Degustaf
  • 2,655
  • 2
  • 16
  • 27
Gaurav
  • 1,005
  • 3
  • 14
  • 33
  • 1
    Doesn't this assume that V will contain a coin worth 1 unit. What if P = 7 and V = [3, 2], you'd end up getting 2 value 3 coins and then error out instead of 1 value 3 coin and 2 value 2 coins. – Louis Ricci Oct 25 '12 at 18:55
  • @LastCoder I got your point. The above solution is assuming that we have a coin worth 1 unit. Now next thing is if we are given coin worth 1 unit, then the above be more efficient solution then the dynamic solution, right? – Gaurav Oct 25 '12 at 19:05
  • Where do you use `Vi` in your formula? – IVlad Oct 25 '12 at 19:09
  • @IVlad: The set is actually Vi to Vj. So when I write Vi,j then i assume that the set V contains all the elements from Vi to Vj. When I write Vi,j-1 then I assume that the set V contains all the elements form Vi to Vj-1 which means that last coin is excluded form the coins set. – Gaurav Oct 25 '12 at 19:13
  • That is a very confusing notation, because you also use just `Vj` and you use commas to separate both indices and function parameters. I suggest using square brackets to make it clearer, I read your formula as `C[p,(Vi),(j-1)]; C[(p-Vj),(Vi),(j)]`. – IVlad Oct 25 '12 at 19:15

2 Answers2

4

Consider P = 6, V = {4, 3, 1}. You would pick 4, 1, 1 instead of 3, 3, so 3 coins instead of the optimal 2.

IVlad
  • 43,099
  • 13
  • 111
  • 179
0

What you've written is similar to the greedy algorithm that works only under certain conditions. (See - How to tell if greedy algorithm suffices for finding minimum coin change?).

Also, in your version you aren't actually using Vi within the recurrence, so it's just a waste of memory

Community
  • 1
  • 1
dfb
  • 13,133
  • 2
  • 31
  • 52