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. SaaS成熟度模型分级:

    SaaS成熟度模型分级: 根据SaaS应用是否具有可配置性,高性能,可伸缩性的特性,SaaS成熟度模型被分成四级.每一级都比前一级增加三中特性中的一种.   可配置 高性能 可伸缩 Level1 N ...

  2. Apache Kafka Replication Design – High level

    参考,https://cwiki.apache.org/confluence/display/KAFKA/Kafka+Replication Kafka Replication High-level ...

  3. Python 3.5 中的异步HTTP请求写法

    Python 3.5 增加了对async def and await的支持,同样的异步代码看起来干净了很多,也更易读. import aiohttp import asyncio async def ...

  4. CentOS中文乱码问题的解决方法

    一.CentOS系统访问 xxx.cn ,发现中文乱码于是用以前的方式:# yum -y install fonts-chinese # yum -y install fonts-ISO8859 Ce ...

  5. Wow! Such Doge!---hdu4847(字符串水题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4847 题意就是求给出的文章中共有多少个doge,不区分大小写直接用strstr做就可以了: #incl ...

  6. 微信小程序登录时序图

    https://developers.weixin.qq.com/miniprogram/dev/api/api-login.html

  7. 【opencv入门篇】 10个程序快速上手opencv【上】

    导言:本系列博客目的在于能够在vs快速上手opencv,理论知识涉及较少,大家有兴趣可以查阅其他博客深入了解相关的理论知识,本博客后续也会对图像方向的理论进一步分析,敬请期待:) PS:官方文档永远是 ...

  8. 【Unity Shader编程】之十六 基于MatCap实现适于移动平台的“次时代”车漆Shader

    本系列文章由@浅墨_毛星云 出品,转载请注明出处.   文章链接:http://blog.csdn.net/poem_qianmo/article/details/55803629 渲染本文配图使用的 ...

  9. python web框架 Django 登录页面

    在django 项目下 创建一个templates 放模板的文件夹 html文件都放在这里 在里面写一个login.html 登录页面 urls.py 加上 login 对应关系 from djang ...

  10. Spring源码解析(一)开篇

    前言 Spring源码继承结构比较复杂,看过以后经常会忘记.因此,记录一下源码分析的过程,方便以后回顾.本次分析的Spring源码版本为3.2.15. 另外,一提Spring就是IOC.DI等等,我们 ...