dp:322. Coin Change 自下而上的dp
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的更多相关文章
- LeetCode OJ 322. Coin Change DP求解
题目链接:https://leetcode.com/problems/coin-change/ 322. Coin Change My Submissions Question Total Accep ...
- [LeetCode] 322. Coin Change 硬币找零
You are given coins of different denominations and a total amount of money amount. Write a function ...
- leetcode@ [322] Coin Change (Dynamic Programming)
https://leetcode.com/problems/coin-change/ You are given coins of different denominations and a tota ...
- 322. Coin Change
动态规划里例题,硬币问题. p[i] = dp[i - coin[j]] + 1; 注意i < coin[j] dp[i-coin[j]]无解都要跳过. public class Solutio ...
- 【LeetCode】322. Coin Change 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 【Leetcode】322. Coin Change
You are given coins of different denominations and a total amount of money amount. Write a function ...
- LeetCode 322. Coin Change
原题 You are given coins of different denominations and a total amount of money amount. Write a functi ...
- 322. Coin Change选取最少的硬币凑整-背包问题变形
[抄题]: You are given coins of different denominations and a total amount of money amount. Write a fun ...
- 322 Coin Change 零钱兑换
给定不同面额的硬币(coins)和一个总金额(amount).写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合方式能组成总金额,返回-1.示例 1:coins = [1, ...
随机推荐
- MYSQL账户是否不允许远程连接。如果无法连接可以尝试以下方法:
mysql账户是否不允许远程连接.如果无法连接可以尝试以下方法: mysql -u root -p //登录MySQL mysql> GRANT ALL PRIVILEGES ON *.* TO ...
- 添加Google网络地图功能
在MeteoInfo中添加了Google网络地图功能.打开MeteoInfo软件,选中图层管理区的一个Map Frame(New Map Frame),点击鼠标右键,在弹出菜单中点击Add Web L ...
- MeteoInfoLab脚本示例:中文处理
在脚本中使用中文需要指明是unicode编码,即在含有中文的字符串前加u,比如:u'中文'.还需要将字体指定为一种中文字体.详见下面的例子.脚本程序: x = [1,2,3,4] y = [1,4,9 ...
- iot平台
iot平台 iot平台卓岚云是一个免费的物联网云平台,用户只需按步骤完成注册即可免费使用卓岚云 .支持PC.Android.iOS多平台终端.用户可以在任何地方远程访问任何地方的串口设备,并对远程设备 ...
- package wang/test is not in GOROOT (/usr/local/go/src/wang/test)
如果要用 gopath模式 引入包 从src目录下开始引入 需要关闭 go mod 模式 export GO111MODULE=off 如果使用go mod 模式 export GO111MODULE ...
- centos8平台基于iftop监控网络流量
一,iftop的作用: 基于ip统计外部机器与本机之间的网络流量, 可以方便的查看各客户端是否有非正常的到本机的访问 说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnbl ...
- centos8平台使用vmstat监控系统
一,vmstat的用途和特点: vmstat 是一个常用的系统性能分析工具,主要用来分析系统的内存使用情况,也常用来分析 CPU 上下文切换和中断的次数. 相对于 iostat 来说,vmstat 可 ...
- 函数-深入JS笔记
代码特点:高内聚,低耦合 耦合 存在执行多个相同作用代码时,这就叫耦合 if (1 > 0) { console.log('a'); } if (2 > 0) { console.log( ...
- JavaWeb宿舍管理系统(附 演示、源码下载地址)
宿舍管理是高校管理的重要组成部分,一套优秀的管理系统不仅可以降低宿舍管理的难度,也能在一定程度上减少学校管理费用的支出,能是建设现代化高校管理体系的重要标志. 本篇文章将带你从运行环境搭建.系统设计. ...
- java数据结构-04单循环链表
单循环链表与单链表的不同是,单循环链表尾结点的next指向第一个结点(或头结点) 代码: 无头结点: public class SingleCircleLinkedList<E> ext ...