题目

题目:CoinChange

有面额不等的coins,数量无限,要求以最少的\(coins\)凑齐所需要的\(amount\)。

若能,返回所需的最少coins的数量,若不能,返回-1。

  • Example 1:

    coins = [1, 2, 5], amount = 11

    return 3 (11 = 5 + 5 + 1)
  • Example 2:

    coins = [2], amount = 3

    return -1.

无法用贪心做,例如:coins = [5,6,10], amount = 11 *。

动态规划解法:

1、

这里我用\(coins=[c_1,c_2,...,c_m]\)表示所有的\(coin\)面值的集合;

用集合\(S(amount)=(c_{i1},c_{i2}...c_{im})\)表示凑齐\(amount\)的一种凑法;

用函数\(f(amount)\)表示 凑齐\(amount\)的所有的凑法。

当\(amount=i\)的时候,有\(f(i)\)种凑法,

$ f(amount) = \{ (c_{i1},...),(c_{i2},...),...,(c_{ik},...) \} ,c_x\in coins$

例如:

\(coins = [1,5,6,9], amount = 11\);

.........\(f(11)=\{ (5,6),(1,1,9)\}\)

凑齐11,有两种办法,一是5+6,另一种是1+1+9.

用\(dp[i]\)表示凑齐\(amount\)所需的最少\(coins\)数,\(dp[i]=-1\)表示无法凑齐。

\(dp[i] = min \{count( f(i) ) \}\)

例如:\(dp[11]=min\{count(f(11))\}=min\{count((5,6),(1,1,9))\}=min\{2,3\}=2\)

2、递推公式:

\(f()\)的递推公式为:

\(f(j)=\{f(i_1)+c_1,f(i_2)+c_2,...,f(i_k)+c_k \}\),条件:\(j>i,f(j)>=0,c_x\in coins\)

\(dp[]\)的递推公式为:

\(dp[j]=min\{dp[i_1]+1,dp[i_2]+1,...,dp[i_k]+1 \}\)

3、边界条件:

\(f(0)=0\)

\(dp[0]=0\)


例子:

amount:                       11
coins: 0 - - - - 5 6 - - - 10 -
amount: 0 1 2 3 4 5 6 7 8 9 10 11
dp: 0 - - - - 1 1 - - - 1 2

代码:

int coinChange(const vector<int>& coins, int amount) {
vector<int> dp(amount + 1, -1);
dp[0] = 0; for (int i = 1; i <= amount ; i++) {
for (int c: coins) {
if (i >= c && dp[i-c] >= 0) { // 若coin面值超过amount,无法凑出
if (dp[i] > 0) { // 若有别的凑法,比较那种凑法用的coins少
dp[i] = dp[i] < (dp[i - c] + 1) ? dp[i] : dp[i - c] + 1;
} else {
dp[i] = dp[i - c] + 1;
}
}
}
}
//display(dp);
return dp[amount];
}

CoinChange的更多相关文章

  1. [LeetCode] Coin Change 硬币找零

    You are given coins of different denominations and a total amount of money amount. Write a function ...

  2. LeetCode Coin Change

    原题链接在这里:https://leetcode.com/problems/coin-change/ 题目: You are given coins of different denomination ...

  3. leetcode bugfree note

    463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...

  4. python面试2

    Python语言特性 1 Python的函数参数传递 看两个例子:     1 2 3 4 5 a = 1 def fun(a):     a = 2 fun(a) print a  # 1 1 2 ...

  5. Coin Change

    You are given coins of different denominations and a total amount of money amount. Write a function ...

  6. leetcode:Coin Change

    You are given coins of different denominations and a total amount of money amount. Write a function ...

  7. leetcode@ [322] Coin Change (Dynamic Programming)

    https://leetcode.com/problems/coin-change/ You are given coins of different denominations and a tota ...

  8. 322. Coin Change

    动态规划里例题,硬币问题. p[i] = dp[i - coin[j]] + 1; 注意i < coin[j] dp[i-coin[j]]无解都要跳过. public class Solutio ...

  9. leetcode & lintcode for bug-free

    刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...

随机推荐

  1. 1.4. chromium源代码分析 - chromiumframe - 消息系列

    Message framework 是对消息循环的封装和扩展,Chromium在消息循环中增加处理内部任务的工作.将内部工作处理寄生在Windows的消息循环中,会有一个问题,就是没有Windows自 ...

  2. SQL Server 取前一天的0点和23点59分59秒

    DECLARE @startDate1 DATE; DECLARE @startDate DATETIME; ,@startDate1); ,CONVERT(DATETIME,@startDate1) ...

  3. Google地图,Baidu地图数据供应商

    http://janwen.iteye.com/blog/488659 Google百度  我老以为百度,Google的地图产品是自己开发的,原来是别人提供的数据, 百度的数据提供商有 北京世纪高通科 ...

  4. 高质量程序设计指南C/C++语言——有了malloc/free为什么还要new/delete?

  5. 驯服你的Windows Server 2003

    虽然通过一些技巧可以让Windows Server 2003更符合我们的使用习惯,但对我等菜鸟来说,操作还是有相当的难度,有没有更简单.更省事的驯服它的办法呢?有,那就是使用Windows Serve ...

  6. 转:说说angularjs中的$parse和$eval

    说说AngularJS中的$parse和$eval AngularJS的初学者常常会对$parse和$eval两个内建服务感到有些困惑,今天我们就来说说AngularJS中的$parse和$eval. ...

  7. JBoss Jopr

    http://rhq.jboss.org/ https://issues.jboss.org/browse/JBPAPP6-947 挺好的网站: http://outofmemory.cn/code- ...

  8. hdu1395-2^x mod n = 1

    http://acm.hdu.edu.cn/showproblem.php?pid=1395 原理为 a ^ b % n == d ; >>>>>>  (( a % ...

  9. uva311 - Packets(贪心)

    题目:311 - Packets 题目大意:给出1*1, 2*2,3 *3, 4*4, 5*5, 6*6的箱子的个数,如今有若干个6*6的箱子,问最少用多少个箱子能够将给定的箱子都装进去. 解题思路: ...

  10. 1.23 确定一个Decimal或Double的整数部分

    知识点: 1.System.Math.PI 2.System.Math.Truncate() //取整 问题: 需要找出一个decimal 或 double数的整数部分. 解决方案 只要将一个deci ...