题目

题目: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. HTTP协议(超文本传输协议)

    一.HTTP的简介: 超文本传输协议. 它是基于TCP连接的(默认端口号是80).所以在传输数据前客户端需向服务器发送连接请求.当服务器同意连接请求,建立连接后才可以发送数据报文. 二.HTTP的报文 ...

  2. .htaccess Rewrite apache重写和配置

    首先: 必须要空间支持 Rewrite 以及对站点目录中有 .htaccess 的文件解析,才有效. 如何让空间支持Rewrite 和 .htaccess 的文件解析呢 往下看 第一步:要找到apac ...

  3. PHP学习笔记12-上传文件

    上传图片文件并在页面上显示出图片 enctype介绍:enctype属性指定将数据发回到服务器时浏览器使用的编码类型. 取值说明: multipart/form-data: 窗体数据被编码为一条消息, ...

  4. 电脑cmos是什么?和bois的区别?

    很多人都分不清电脑cmos和bois区别,有人一会儿说什么bois设置,有人一会儿说cmos设置.而看起来这两个又似乎差不多,本文将用最简单的白话文告诉各位,什么是cmos,以及cmos和bois的的 ...

  5. 安装 Rational Rose 启动报错:无法启动此程序,因为计算机中丢失 suite objects.dll

    安装完以后提示找不到 suite objects.dll: 经查找,该 dll 存在: 找不到的原因是,安装程序自动设置在 Path 中的环境变量有误: 把最后的 common 改成 Common: ...

  6. 文件的哈希值不在指定的目录文件中。此文件可能已损坏或被篡(Windows10 /Windows8.1)

    ------------------------------------------Windows10------------------------------------------------ ...

  7. 细说SSO单点登录(转)

    什么是SSO? 如果你已知道,请略过本节! SSO核心意义就一句话:一处登录,处处登录:一处注销,处处注销.即:在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统. 很多人容易把SS ...

  8. 导出Ext.grid.Panel到excel

    1.客户端定义,基本的想法是form提交表格头定义,数据,以json方式传输 Ext.grid.Panel.addMembers({ exportExcel:function(options){ if ...

  9. QT在ui文件上建立信号操机制会不会对后期维护产生影响 - love4Mario的专栏 - 博客频道 - CSDN.NETQT在ui文件上建立信号操机制会不会对后期维护产生影响 - love4Mario的专栏 - 博客频道 - CSDN.NET

    QT在ui文件上建立信号操机制会不会对后期维护产生影响 - love4Mario的专栏 - 博客频道 - CSDN.NET QT在ui文件上建立信号操机制会不会对后期维护产生影响 分类: 学习心得 2 ...

  10. CSS样式属性

    一.背景与前景 1.背景: 2.前景字体: (二)边界和边框 border(表格边框.样式等).margin(表外间距).padding(内容与单元格间距). margin(外边距)对自身不会有变化, ...