0

I'm confused as to what the matrix should look like for the dynamic programming technique of the coin problem. Say I have the denominations of 1c,5c,10c and 25c and i call the make-change(10). i.e. I want to make change for 10 cents, what should my final, matrix/array look like. I need to know this as I want to assign an array in the beginning of the program. I'm not looking for the code here.

Phoenix
  • 8,695
  • 16
  • 55
  • 88

2 Answers2

2

You could use a matrix where one dimension shows the sum for which you're calculating right now, and the other shows which coins you can use - for example, the first row shows the number of ways to achieve a given sum using 1c coins only, the second row - 1c and 5c, the third - 1c, 5c and 10c and so on.

So in your example, it could look like this:

Sum:  0 1 2 3 4 5 6 7 8 9 10
Ways: 1 1 1 1 1 1 1 1 1 1 1   (using only 1c coins - N x 1c)
      1 1 1 1 1 2 2 2 2 2 3   (using 1c and 5c coins - either Nx1c, 1x5c + (N - 5)*1c or 2x5c)
      1 1 1 1 1 2 2 2 2 2 4   (using 1c, 5c and 10c coins - one of the above, or 1x10c)

You don't really need to store the whole matrix though - the last row should always be enough.

Explanation for getting rid of most of the matrix:

Suppose you've found the answer when using only 1c coins (which is not a terribly hard thing to do) and it's stored in a single array, say DP. Now, you have this information and you want to know the answer for 1c and 5c coins.

You can proceed from sum = 0 to 10, maintaining the following invariant:

  • when you're calculating the answer for sum, the entry in DP for each number x between 0 and sum - 1 inclusive shows the number of ways to get sum using 1c and 5c coins. All the rest of the entries (from sum to the end of the array) contain the answer when using 1c coins only.

And maintaining it is actually pretty easy: when you're calculating the answer for x, you know:

  • the number of ways to get x using only 1c coins - this is DP[x], since we still haven't overwritten it;
  • the number of ways to get x using at least one 5c coin - that's DP[x - 5], i.e. the number of ways to get the number of coins that remains after we remove 5c (or 0, if x - 5 < 0).

Then you can just sum those two numbers and store the result in DP[x]. Then continue for the rest of the coins analogously.

Ivan Vergiliev
  • 3,771
  • 24
  • 23
  • why don't i need to store the whole matrix ? why is the last row enough ? can you give an example with 10 cents for how the last row will be enough. – Phoenix Oct 02 '12 at 21:26
  • See edited answer - it got a bit long, but I hope it's clear. – Ivan Vergiliev Oct 02 '12 at 21:54
  • Hi Ivan. can you show how dp[10] will be calculated here, instead of just for x. how will dp[10] come out to be 4 – Phoenix Oct 02 '12 at 23:15
  • Try simulating the algorithm I've described by hand - it will help you understand it better. Do it for some smaller numer first, maybe even with smaller coins (e.g. 1c, 2c, 5c). Then you can let me know if you have any problems. – Ivan Vergiliev Oct 02 '12 at 23:20
  • If you still haven't figured that out - the number of ways to get 10 using 1c, 5c and 10c coins - let's call this ways(10, 3) - first is the sum, then the number of different coins we're using - is: ways(10, 3) = ways(10 - 10, 3) + ways(10, 2) - the first one is "take one 10c coin, in how many ways can you get what's left", the second is the number of ways to get a 10 using 1c and 5c coins only. ways(0, 3) = 1, ways(10, 2) = 3 by similar logic. – Ivan Vergiliev Oct 07 '12 at 14:26
0

I think a hashmap that maps an amount to a list of coins. So it would look something like:

{0 => (),
 1 => (1),
 2 => (1,1),
 3 => (1,1,1),
 ...
 13 => (10,1,1,1)
 ...
}
ajon
  • 7,868
  • 11
  • 48
  • 86