题目

题目: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. Android应用开发提高篇(3)-----传感器(Sensor)编程

    链接地址:http://www.cnblogs.com/lknlfy/archive/2012/02/29/2373420.html 一.概述 Android支持的传感器种类越来越多了,这确实是一件可 ...

  2. BZOJ 1601: [Usaco2008 Oct]灌水( MST )

    MST , kruskal 直接跑 ---------------------------------------------------------------------- #include< ...

  3. Dojo实现Tabs页报错(二)

  4. python自学笔记(十一)关于函数及书写格式

    1.函数是抽象的第一步       1.1 有关高压锅     1.2 函数是抽象出来的结构,是总结,是方法     1.3 多用函数     2.如何定义函数        2.1 def是关键词, ...

  5. HTML+CSS笔记 CSS进阶

    文字排版 字体 我们可以使用css样式为网页中的文字设置字体.字号.颜色等样式属性. 语法: body{font-family:"宋体";} 这里注意不要设置不常用的字体,因为如果 ...

  6. 有意思的C宏

    在Linux内核.嵌入式代码等传统的C代码里,会有一些难以识别的宏定义.我记得在eCos, UBoot, FFmpeg有一些比较BT的宏定义,很难读懂.对于C++程序员来说,最好将这种难读的宏定义转成 ...

  7. 网站流量统计系统 phpMyVisites

    phpMyVisites是一个网站流量统计系统,它能够提供非常详细的统计报告和高级图形报表.phpMyVisites不是一个Apache log分析工具,它建有自己的log.它的特点包括: 安装部署: ...

  8. C# Windows Sockets (Winsock) 接口 (转)

    在.Net中,System.Net.Sockets 命名空间为需要严密控制网络访问的开发人员提供了 Windows Sockets (Winsock) 接口的托管实现.System.Net 命名空间中 ...

  9. python字符串操作总结

    python中有各种字符串操作,一开始python有个专门的string模块,要使用需先import string.后来从python2.0开始,string方法改用str.method()形式调用, ...

  10. 介绍一个python的新的web framework——karloop框架

    karloop是一款轻型的web framework,和tornado.webpy类似.mvc分层设计,眼下已经公布早期版本号了,使用方便, 下载地址例如以下:https://github.com/k ...