题目

题目: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. 1028: C语言程序设计教程(第三版)课后习题8.2

    Description求方程 的根,用三个函数分别求当b^2-4ac大于0.等于0.和小于0时的根,并输出结果.从主函数输入a.b.c的值.Inputa b cOutputx1=? x2=?Sampl ...

  2. printf参数的问题

    根据前面的某一篇的文章,可以清楚的看到:对于每一个函数,通过这个函数的[ebp+x]就可以直接访问到它调用的时候传进来的形参的值,通过[ebp-x]就可以直接访问它的局部变量. 所以printf这个函 ...

  3. Sqoop mysql 数据导入到hdfs

    1.--direct 模式使用mysqldump 工具,所以节点上需要安装该工具,非direct 模式直接使用jdbc ,所以不需要 具体script参考如下: sqoop import --conn ...

  4. html5储存篇(二)

    indexedDB   相对于html5 中提到 web SQL Database,w3c已经明确声明放弃对其的继续支持,开始支持新的客户端数据库 indexedDB ,indexedDB 是一种no ...

  5. C-重定向

    说实话,第一次接触重定向这一个概念,感觉是那么的神奇简洁不可思议…………………… freopen() 本来应该是打开的是文件指针,但是分配了指针,使她(亲切)指向了标准输入.输出.错误流. 用 法: ...

  6. Sublime Text2

    Ctrl+L选择整行(按住-继续选择下行) Ctrl+KK 从光标处删除至行尾 Ctrl+Shift+K 删除整行 Ctrl+Shift+D 复制光标所在整行,插入在该行之前 Ctrl+J 合并行(已 ...

  7. Spring RESTful服务接收和返回JSON最佳实践

    http://blog.csdn.net/prince_hua/article/details/12103501

  8. XML中SystemID和PublicID的区别

    http://hi.baidu.com/binboot007/item/1533f91d52113d7c7b5f259c http://supportweb.cs.bham.ac.uk/documen ...

  9. Protel 99SE PCB 打印技巧

    1. 打开 Protel99SE PCB 设计文档.从菜单File 下单击Print/Preview 打印预览菜单.出现PCB 打印预览介面. 2.从File 下单击 Setup Printer 设置 ...

  10. Spring Boot普通类调用bean

    1 在Spring Boot可以扫描的包下 假设我们编写的工具类为SpringUtil. 如果我们编写的SpringUtil在Spring Boot可以扫描的包下或者使用@ComponentScan引 ...