题目

题目: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. mysql中随机取出几条数据

    SELECT t1.id,title,extName,cover,url FROM shop_articles AS t1 JOIN (SELECT ROUND(RAND() * ((SELECT M ...

  2. C#中小函数的应用

    今天看到的一段函数 StringBuilder sb = new StringBuilder("sselect * from table where 1=1"); if(TextB ...

  3. Fedora 17配置ssh及Windows远程连接

    转载自:http://nanjingjiangbiao-t.iteye.com/blog/1794213 Fedora 23 默认已经安装好openssh server了,不用再装不过默认情况下没有开 ...

  4. c语言各类问题 代码

    定义一个结构体,有两个成员变量,一个整型的n,一个字符型的c,利用结构体类型声明一个具有5个元素的数组,并随机初始化,根据成员变量n进行从小到大排序,然后输出 冒泡排序然后 在输出结构体#includ ...

  5. 【转】Win7下VS2010中配置Opencv2.4.4的方法(32位和64位都有效)(亲测成功)

    在vs2010下配置opencv是件痛苦的事情,一点点错误可能就会导致莫名其妙的报错,各种error让人郁闷不已,这里提供给大家一篇vs2010下配置opencv2.4.4的方法,我是64位的win7 ...

  6. [编程语言][java][java se]java泛型中? T K V E含义(学习)

    ? 表示不确定的java类型,类型是未知的. T  表示java类型. K V 分别代表java键值中的Key Value. E 代表Element,特性是枚举. 1.意思     jdk中的K,V, ...

  7. hadoop高速扫盲帖,从零了解hadoop

    1.MapReduce理论简单介绍 1.1 MapReduce编程模型 MapReduce採用"分而治之"的思想,把对大规模数据集的操作,分发给一个主节点管理下的各个分节点共同完毕 ...

  8. C#中字符串的处理,对象的引用及继承(Tenth day)

    又进入到了新的一周,现在到总结的时间了,继续为大家总结一下今天在云和学院所学的知识. 理论: StringBuilder 和 String 的区别    String 在进行运算时(如赋值.拼接等)会 ...

  9. 密封关键字sealed

    在两种情况下使用: ·不想让别人继承:例如public sealed class Person{}; ·不想让子类重写自己的方法 例如: public class Person{ public vis ...

  10. eclipse修改java代码后报错: java.lang.OutOfMemoryError: PermGen space

    由于在eclipse中运行项目后,我们又重新修改了某个java类,导致tomcat会重新加载这个项目所有的class.jar,多次加载后由于分配的存储空间有限,就导致了:java.lang.OutOf ...