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. flask系列

    1.flask基础 2.flask上下文 3.flask源码剖析--请求流程 4.数据库连接池DButils 5.Flask-Session 6.WTForms 7.Flask-SQLAlchemy ...

  2. ISO C++标准委员会不是一个一般意义上权力机构,基本上愿意交会费,愿意自己出时间,出酒店机票,出提案,就可以申请加入。

    ISO C++标准委员会不是一个一般意义上权力机构,基本上愿意交会费,愿意自己出时间,出酒店机票,出提案,每年全世界参加会议被专家巨细靡遗地评头论足,就可以申请加入. 所以参加标准委员会背景各异,有人 ...

  3. 【opencv】cv::Mat_ 对单个元素赋值

    创建一个cv::Mat_并赋值 cv::Mat_<,); mat(,)=VIRTUAL_FOCAL; mat(,)=; mat(,)=roiSize_x/; mat(,)=; mat(,)=VI ...

  4. MySQL单列索引和组合索引的区别介绍(转)

    原文:http://database.51cto.com/art/201011/233234.htm MySQL单列索引是我们使用MySQL数据库中经常会见到的,MySQL单列索引和组合索引的区别可能 ...

  5. Linux more命令

    more命令类似与cat命令,却比cat命令强大,它以全屏幕的方式按页显示文本文件的内容,支持vi中的关键字定位操作. 1.快捷键 space, z 向下翻页b,ctrl+b       向上翻页 E ...

  6. 前端 javascript 定时器

    setInterval("执行的代码",间隔时间)毫秒单位 每5秒一次会提示出弹框 <!DOCTYPE html> <html lang="en&quo ...

  7. 微信对接HIS——微信可查检验结果

    患者仅仅要关注医院官方微信,不管身处何地,输入自己预留在医院的电话号码.检验单的条码号,就能够了解检验结果. 医院信息系统在提供病人数据信息前,会对查询方做身份认证和安全防护检測,录入患者挂号时预留的 ...

  8. python学习笔记(七)操作mysql

    python操作mysql数据库需要先安装pymysql模块,在之前博客中可翻看如何安装,最简单的就是pip install pymysql 1.导入pymysql模块   import pymysq ...

  9. SpringBoot的核心注解和配置

    一.入口类和SpringBootApplication Spring Boot的项目一般都会有*Application的入口类,入口类中会有main方法,这是一个标准的Java应用程序的入口方法. @ ...

  10. 关联规则之Apriori

    1.关联规则原理 1.关联规则概述 关联规则(Association Rules)是反映一个事物与其他事物之间的相互依存性和关联性,如果两个或多个事物之间存在一定的关联关系,那么,其中一个事物就能通过 ...