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. netty 网关 flume 提交数据 去除透明 批处理 批提交 cat head tail 结合 管道显示行号

    D:\javaNettyAction\NettyA\src\main\java\com\test\HexDumpProxy.java package com.test; import io.netty ...

  2. 全量日志 requestId

    常量参数和系统参数 API 的请求者不可见,由网关在请求后端服务时添加上. 常量参数.比如您的后端需要接收一个常量,但是这个常量您不希望被您的客户看见,那么就设置一个常量参数,可以在 Header 或 ...

  3. Azkaban简介及使用

    一.Azkaban概述 Azkaban是一个分布式工作流管理器,在LinkedIn上实现,以解决Hadoop作业依赖性问题. 我们有需要按顺序运行的工作,从ETL工作到数据分析产品. 特点: 1)给用 ...

  4. vue监听浏览器窗口大小变化

    首先,页面初始化mounted的时候,通过 document.body.clientWidth 和 document.body.clientHeight 来获取到浏览器的宽和高,然后通过 window ...

  5. 用 Python 实现每秒处理 120 万次 HTTP 请求

    用 Python 做到每秒处理上百万次 HTTP 请求,可能吗?也许不能,但直到最近,这已成为现实. 很多公司都在为了提升程序的执行性能和降低服务器的运营成本,而放弃 Python 去选择其它编程语言 ...

  6. linux系统压缩\解压命令详解

    转自:http://www.cnblogs.com/qq78292959/archive/2011/07/06/2099427.html. tar -c: 建立压缩档案-x:解压-t:查看内容-r:向 ...

  7. 模块讲解----shutil模块(copy、压缩、解压)

    作用与功能 主要用于文件的copy,压缩,解压 导入shuitl模块: import shutil copy方法 1.shutil.copyfileobj()  打开file1,并copy写入file ...

  8. memento模式

    参考资料 • 维基百科:https://en.wikipedia.org/wiki/Memento_pattern • 百度百科:http://baike.baidu.com/link?url=ZQZ ...

  9. webService 总结

    Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量级的 ...

  10. vue工程权限怎么配置?

    vue工程权限怎么配置? router.beforeEach((to, from, next) => { }):方法的to参数能拿到router设置的对象信息,如: { path: " ...