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. 用ip xfrm搭ipsec隧道

    拓扑如下 基本的IP配置就不说了,直接写重点,在LS上配置 #配置SA ip xfrm state add src 194.168.10.4 dst 194.168.10.5 proto esp sp ...

  2. empty()和size() == 0有区别吗

    empty()和size() 这里说的empty()和size()都是STL的容器中提供的接口,分别用来判断当前容器是否为空和获取当前包含的元素个数 区别 其实按道理来说两者应该是相等的,而且STL容 ...

  3. Python面试题-数据库相关

    1.mysql如何做分页 mysql数据库做分页用limit关键字,它后面跟两个参数startIndex和pageSize 2.mysql引擎有哪些 innodb和myisam两个引擎,两者区别是 i ...

  4. BASH提示符颜色、显示返回值,终端标题显示当前目录与正在执行的命令

    BASH的PS1变量控制提示符相关的东西,善用它可以让BASH用起来舒服很多 提示符颜色 提示符显示上一个命令的返回值(exit code),并根据是否0调整颜色 提示符生成的时间(这样就知道上一条命 ...

  5. spring boot:构建多模块项目(spring boot 2.3.1)

    一,为什么要使用多模块? 1,结构更清晰,方便管理    如果只是一个小项目当然没有问题,    但如果功能越增越多则管理越来越复杂,    多模块可以使项目中模块间的结构分离   2,把项目划分成多 ...

  6. linux(centos8):安装prometheus服务端/node_exporter客户端(prometheus 2.18.1)

    一,prometheus的用途 Prometheus是一个开源的系统监控和警报工具包 相比其他监控系统,它更适用于微服务的体系架构 它使用各种专用exporter,用来实现对硬件/存储/数据库/web ...

  7. centos8平台使用nethogs基于进程监控网络流量

    一,nethogs的作用: 按进程或程序实时统计网络带宽使用率 我们查看流量的占用时,知道来源的ip.访问的端口,还不足以帮我们确认到进程, 而nethogs则可以让我们查看每个进程所占用的流量带宽 ...

  8. centos8平台使用dnf/yum管理软件包

    一,dnf的用途 centos7开始,DNF 成为了默认的软件包管理器,同时 yum 仍然是可用的 DNF包管理器克服了YUM包管理器的一些瓶颈,提升了用户体验,内存占用,依赖分析,运行速度等方面 D ...

  9. windows搭建SVN服务MD版

    windows搭建SVN服务MD 1下载TortoiseSVN 官网下载 根据自己系统环境选择适合的版本 2 安装TortoiseSVN 双击运行程序 出现第一个小坑 原来是你的系统没有打 kb299 ...

  10. JavaScript实现异步的4中方法

    一:背景简介 Javascript语言的执行环境是"单线程"(single thread). 所谓"单线程",就是指一次只能完成一件任务.如果有多个任务,就必须 ...