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.

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

Example 1:

Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1

Example 2:

Input: coins = [2], amount = 3
Output: -1

Example 3:

Input: coins = [1], amount = 0
Output: 0

Example 4:

Input: coins = [1], amount = 1
Output: 1

Example 5:

Input: coins = [1], amount = 2
Output: 2

Constraints:

  • 1 <= coins.length <= 12
  • 1 <= coins[i] <= 231 - 1
  • 0 <= amount <= 104
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++){
//dp[i]初值都是amount+1
if(i>=coins[j]) dp[i] = min(dp[i-coins[j]]+1,dp[i]);
}
}
return dp[amount] < amount+1 ? dp[amount]:-1;
}
};

dp:322. Coin Change 自下而上的dp的更多相关文章

  1. LeetCode OJ 322. Coin Change DP求解

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

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

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

  3. leetcode@ [322] Coin Change (Dynamic Programming)

    https://leetcode.com/problems/coin-change/ You are given coins of different denominations and a tota ...

  4. 322. Coin Change

    动态规划里例题,硬币问题. p[i] = dp[i - coin[j]] + 1; 注意i < coin[j] dp[i-coin[j]]无解都要跳过. public class Solutio ...

  5. 【LeetCode】322. Coin Change 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  6. 【Leetcode】322. Coin Change

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

  7. LeetCode 322. Coin Change

    原题 You are given coins of different denominations and a total amount of money amount. Write a functi ...

  8. 322. Coin Change选取最少的硬币凑整-背包问题变形

    [抄题]: You are given coins of different denominations and a total amount of money amount. Write a fun ...

  9. 322 Coin Change 零钱兑换

    给定不同面额的硬币(coins)和一个总金额(amount).写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合方式能组成总金额,返回-1.示例 1:coins = [1, ...

随机推荐

  1. Mac快捷键整理(随时更新)

    一.文字编辑 快捷键 含义 解释 control + A 到行首 A没找到对应单词,暂时认为A是字母表的开始 control + E 到行尾 E是end的缩写

  2. day32 Pyhton 模块02复习 序列化

    一. 什么是序列化 在我们存储数据或者网络传输数据的时候. 需要对我们的对象进行处理. 把对象处理成方便存储和传输的数据格式. 这个过程叫序列化 不同的序列化, 结果也不同. 但是目的是一样的. 都是 ...

  3. 打印一个N*N的方阵,N为每边字符的个数( 3〈N〈20 ),写出来真是泪牛满面啊。

    本程序可以完美输出,效果如下: 相信你已经很期待代码实现了吧,其实代码真的不难,关键是思想.我说说我的想法吧,首先我构造了一个数组,保存了上面图片中字符一行的一半.聪明的你肯定看出来了,上面多数字符都 ...

  4. centos7虚拟机时间和本地时间相差8小时

    安装ntp和ntpdate 在安装centos7虚拟机的时候,已经将时区设置为了Asia/shanghai,但还是出现时间不准,相差了8小时 可以安装ntp和ntpdate,使用 NTP 公共时间服务 ...

  5. 10年经验17张图带你进入gitflow企业项目代码版本管理的最佳实践

    前言 对于项目版本管理,你是否存在这样的痛点:项目分支多而杂不好管理,git log界面commit信息错乱复杂无规范,版本回退不知道选择什么版本合适--. 项目版本管理的最佳实践系列,笔者将以两篇文 ...

  6. 扫描仪扫描文件处理-ABBYY生成小体积黑白二值化PDF

    禁止所有预处理选项: PDF保存选项: 保存提示(选择"保存为仅图像PDF"):

  7. HDU-1051 Wooden Sticks--线性动归(LIS)

    题目大意:有n根木棍(n<5000),每根木棍有一个长度l和重量w(l,w<10000),现在要对这些木头进行加工,加工有以下规则: 1.你需要1分钟来准备第一根木头. 2.如果下一根木头 ...

  8. spring boot:用rocketmq消息订阅实现删除购物车商品功能(spring boot 2.3.3)

    一,为什么要使用消息队列实现删除购物车商品功能? 消息队列主要用来处理不需要立刻返回结果的业务, 常见的例子: 用户在下单后,要清除原购物车中的商品, 这个处理过程不需要马上实现也不需要返回结果给用户 ...

  9. centos8安装fastdfs6.06集群方式一之:软件下载与安装

    一,查看本地centos的版本 [root@localhost lib]# cat /etc/redhat-release CentOS Linux release 8.1.1911 (Core) 说 ...

  10. linux(centos8):用uniq去除文本中重复的行(去重)

    一,uniq命令的用途 1, 作用: 从输入文件或标准输入中找到相邻的匹配行, 并写入到输出文件或标准输出 2, 使用时通常会搭配sort使用 说明:刘宏缔的架构森林是一个专注架构的博客,地址:htt ...