Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
528 views
in Technique[技术] by (71.8m points)

algorithm - Coin change problem with infinite number of coins of each denomination

I want to know the idea of algorithm for the coin change problem where each denomination has infinte number of coins. Means how to apply DP (like the standard coin change problem) For e.g in set 1,10,15, change for 35 gives--2 coins of 10 and one coin of 15

Also give me an idea of brute forcing algorithm for this. I know to iterate over all the sets. But how to vary the number of each coin while brute forcing

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I would think about building the solution one step at a time, inductively:

Coins available are 1c, 5c, 10c, 25c (you can tweak them according to your needs)

  1. Minimun coins for 1c = 1 X 1c. Upto 4 cents, we need 1c coins, as that is the least denomination.
  2. For 5 cents, we have one 5c coin. Combining that with 4c above, we can generate any number between 1 and 9.
  3. For 10 cents, we need 1 X 10c. Combining the above three, we can generate any number between 1 and 19.
  4. For 20c, we need 2 x 10c, as 20 is divisible by 10.

If you can formulate the problem inductively, it might be easier to tackle it.

EDIT:
Alright, here's another attempt to explain the dynamic programming solution:

Think of a table with x rows (x is number of distinct denominations) and n columns (n is the amount you have to build using least denominations). Every cell in this table represents a distinct sub-problem and will eventually contain the solution to it. Assume:

row 1 represents the set {1c} i.e. in row 1 you are allowed to use infinite 1c
row 2 represents the set {1c, 10c} i.e in row 2 you are allowed to infinite 1c and 10c
row 3 represents the set {1c, 10c, 15c} and so on...
Each column represents the amount you want to construct.

Thus, every cell corresponds to one small sub-problem. For example (the indexes are starting from 1 for the sake of simplicity),
cell(1, 5) ==> construct 5c using only {1c}
cell(2, 9) ==> construct 9c using {1c, 10c}
cell(3, 27) ==> construct 27c using {1c, 10c, 15c}
Now your aim is to get the answer to cell(x, n)

Solution:
Start solving the table from the simplest problem. Solving the first row is trivial, since in the first row the only denomination available is {1c}. Every cell in row 1 has a trivial solution, leading to cell(1, n) = {nx1c} (n coins of 1c).

Now proceed to the next row. Generalizing for the 2nd row, lets see how to solve for (say) cell(2, 28) i.e. construct 28c using {1c, 10c}. Here, you need to make a decision, whether to include 10c in the solution or not, and how many coins. You have 3 choices (3 = 28/10 + 1)

Choice 1:
Take {1x10c} and the rest from the previous row (which is stored in cell(1, 18)). This gives you {1x10c, 18x1c} = 19 coins

Choice 2:
Take {2x10c} and the rest from previous row (which is stored in cell(1, 8)). This gives you {2x10c, 8x1c} = 10 coins

Choice 3:
Take no 10c and the rest from the previous row (which is stored in cell(1, 28)). This gives you {28x1c} = 28 coins

Clearly, choice 2 is the best as it takes less coins. Write it down in the table and proceed ahead. The table is to be filled one row at a time, and within a row, in the order of increasing amounts.

Going by above rules, you will reach cell(x, n), the solution to which will be a choice between n/p + 1 alternatives, where p = newest denomination in row x. The best choice is your answer.

The table actually memoizes the solutions to smaller problems, so that you don't need to solve them again and again.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...