10/28/2018 Minimal Coin Problems (最小硬币问题)

                                                                              Minimal Coin Problem(DP)

Problem Statement: 

Given an amount of money n, (n >= 1) and a list of coins (the number of each type of coin is unlimited), what is the minimal number of coins whose total value is equal to the amount of money n?

 

Analysis: 

This is a very classic dynamic programming problem, and we can use the strategy we discuss in the class to solve general dp problems to solve this problem. From class, we discuss that the strategy to solve dp problem is:

  1. Identify the recursive structure of the problem
  2. Select a good order for solving subproblems
  3. Save solution to each subproblem in memory

In this problem, we can identify that there is an optimal substructure. In order to get the minimal number of coins for value i, the number of coins to get value j (where j < i and we assume that j can be summed up by the coins given) must also be minimal, or there is a better alternative. We define F(i) as the minimal number of coins to add up to value i. Then we have this recurrence relationship 

                                    F(i) = min(F(i – coinValue(j)) + 1)

where i is the current value of money and i >= coinValue(j)

 

The base case is F(0) = 0, which is obvious because we don’t need any coin to achieve value 0. With regard to the order for solving subproblems, I chose to use bottom-up dp, in which I first solve the minimal number of coins needed to constitute the less value, and get the answer for the larger values from the previous results. Finally, after solving each subproblem, I store the result into the dp table for the later use. 

 

Implementation: 

Below is the snapshot of my Java implementation to solve minimal coin problem:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值