Dive into Coin Changing Problem


c、With actual U.S. coins, we can use coins of denomination 1, 10, and 25. When
n = 30 cents, the greedy solution gives one quarter and five pennies, for a total
of six coins. The non-greedy solution of three dimes is better.
     The smallest integer numbers we can use are 1, 3, and 4. When n = 6 cents, the
greedy solution gives one 4-cent coin and two 1-cent coins, for a total of three
coins. The non-greedy solution of two 3-cent coins is better.


d、Since we have optimal substructure, dynamic programming might apply. And
indeed it does.
      Let us define c[ j ] to be the minimum number of coins we need to make change
for j cents. Let the coin denominations be d1, d2, . . . , dk . Since one of the
coins is a penny, there is a way to make change for any amount j ≥ 1.
Because of the optimal substructure, if we knew that an optimal solution for
the problem of making change for j cents used a coin of denomination di , we
would have c[ j ] = 1 + c[ j − di ]. As base cases, we have that c[ j ] = 0 for all
j ≤ 0.
      To develop a recursive formulation, we have to check all denominations, giving


c[ j ] = 0                                                                     if j ≤ 0 ,
          1 + min{c[ j − di ]} (among which , 1≤i≤k)           if j > 1 .


      We can compute the c[ j ] values in order of increasing j by using a table.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值