Question

You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.

Example 1:

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

return 3 (11 = 5 + 5 + 1)

Example 2:

coins = [2], amount = 3

return -1.

Note:

You may assume that you have an infinite number of each kind of coin.

Credits:

Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

Solution

动态规划。依次计算出组成1~amount所需要的硬币数目。

dp[i] = min(dp[i], dp[i - coins[j]] + 1),因为是依次求解的,那么求dp[i]的时候,dp[i - coins[j]]已经求解过了。

Code

class Solution {
public:
int coinChange(vector<int>& coins, int amount) {
vector<int> dp(amount + 1, amount + 1);
dp[0] = 0;
for (int i = 1; i <= amount; i++) {
for (int j = 0; j < coins.size(); j++) {
if (coins[j] <= i) {
dp[i] = min(dp[i], dp[i - coins[j]] + 1);
}
}
}
return dp[amount] < amount + 1 ? dp[amount] : -1;
}
};

LeetCode——Coin Change的更多相关文章

  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 2 硬币找零之二

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

  3. LeetCode Coin Change

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

  4. LeetCode OJ 322. Coin Change DP求解

    题目链接:https://leetcode.com/problems/coin-change/ 322. Coin Change My Submissions Question Total Accep ...

  5. [LeetCode] 518. Coin Change 2 硬币找零之二

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

  6. [LeetCode] 322. Coin Change 硬币找零

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

  7. [LeetCode] 518. Coin Change 2 硬币找零 2

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

  8. 【LeetCode】518. Coin Change 2 解题报告(Python)

    [LeetCode]518. Coin Change 2 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目 ...

  9. HDOJ 2069 Coin Change(母函数)

    Coin Change Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

随机推荐

  1. SNORT--install ---dependency-resolve

    # ./configure 遇到ERROR: checking for pfring_open in -lpcap... no ERROR!  Libpcap library/headers (lib ...

  2. (转载)移动Web开发技巧汇总

    META相关 1. 添加到主屏后的标题(IOS) <meta name="apple-mobile-web-app-title" content="标题" ...

  3. python基础-第五篇-5.4正则表达式

    正则基础知识 正则表达式是通过调用re模块实现的 在python里,正则表达式处理对象为字符串,所以正则里方法和字符串的方法有很多相似的地方:re.findall和find,re.split和spli ...

  4. window7修改hosts文件

    以管理员身份登录系统 ,修改 C:\Windows\System32\drivers\etc\hosts文件, 在最下面加入类似 192.168.80.10 master192.168.80.11 s ...

  5. SKBUFFER详解

    纯属转载,不敢侵犯别人产权!! 一. SKB_BUFF的基本概念1. 一个完整的skb buff组成(1) struct sk_buff--用于维护socket buffer状态和描述信息(2) he ...

  6. sql server 测试delete后数据空间情况

    总结结论: [1]如果是索引组织表,删除的数据空间是会被文件设置为可用状态,其他表都可以使用. [2]如果是堆表,删除数据空间也会设置为可用状态,但是只能给被删除数据的表使用. [3]truncate ...

  7. instanceof判断参数是否是给定的类型

    if(ofj instanceof CLOB) {//判断ofj是否是CLOB类型,如果是则把CLOB内容解析出来,放入TZNR字段中并返回 CLOB ft = (CLOB)ofj; String c ...

  8. ffmpeg,rtmpdump和nginx rtmp实现录屏,直播和录制

    公司最近在做视频直播的项目,我这里分配到对直播的视频进行录制,录制的方式是通过rtmpdump对rtmp的视频流进行录制 前置的知识 ffmpeg: 用于实现把录屏工具发出的视频和音频流,转换成我们需 ...

  9. python学习笔记(十一)redis的介绍及安装

    一.redis简介 1.redis是一个开源的.使用C语言编写的.支持网络交互的.可基于内存也可持久化的Key-Value数据库.       2.redis的官网地址,非常好记,是redis.io. ...

  10. 脚本其实很简单-windows配置核查程序(2)

    bat脚本是什么? 首先讲讲什么是命令行,在windows操作系统中,点击左下角的win图标,直接输入cmd搜索,左键点击进入命令行模式(或按键盘上的win键+r直接调出来命令行窗口). 在windo ...