当前位置:网站首页>LeetCode_518_零钱兑换Ⅱ
LeetCode_518_零钱兑换Ⅱ
2022-08-01 23:53:00 【Fitz1318】
题目链接
题目描述
给你一个整数数组 coins
表示不同面额的硬币,另给一个整数 amount
表示总金额。
请你计算并返回可以凑成总金额的硬币组合数。如果任何硬币组合都无法凑出总金额,返回 0
。
假设每一种面额的硬币有无限个。
题目数据保证结果符合 32 位带符号整数。
示例 1:
输入:amount = 5, coins = [1, 2, 5]
输出:4
解释:有四种方式可以凑成总金额:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
示例 2:
输入:amount = 3, coins = [2]
输出:0
解释:只用面额 2 的硬币不能凑成总金额 3 。
示例 3:
输入:amount = 10, coins = [10]
输出:1
提示:
1 <= coins.length <= 300
1 <= coins[i] <= 5000
coins
中的所有值 互不相同0 <= amount <= 5000
解题思路
动态规划五部曲
- 确定
dp
数组及下标含义dp[j]
:数量为j
时的组合数量
- 确定递推公式
dp[j] += dp[j - coins[i]]
- dp数组初始化
dp[0] = 1
- 确定遍历顺序
AC代码
class Solution {
public int change(int amount, int[] coins) {
int[] dp = new int[amount + 1];
dp[0] = 1;
for (int i = 0; i < coins.length; i++) {
//遍历物品
for (int j = coins[i]; j <= amount; j++) {
//遍历背包容量
dp[j] += dp[j - coins[i]];
}
}
return dp[amount];
}
}
边栏推荐
- The third chapter of the imitation cattle network project: develop the core functions of the community (detailed steps and ideas)
- 切面打印调取的方法
- Convert LocalDateTime to Date type
- CDH6的Hue打开出现‘ascii‘ codec can‘t encode characters
- numpy.around
- 一款简洁的文件传输工具
- ICLR 2022 Best Paper: Partial Label Learning Based on Contrastive Disambiguation
- Various Joins of Sql
- ansible模块--copy模块
- 2022第六届强网杯部分wp
猜你喜欢
Sql之各种Join
经典文献阅读之--DLO
使用Jenkins做持续集成,这个知识点必须要掌握
Secondary Vocational Network Security Competition B7 Competition Deployment Process
sys_kill system call
Chrome书签插件,让你实现高效整理
2022 6th Strong Net Cup Part WP
多御安全浏览器android版更新至1.7,改进加密协议
Use Jenkins for continuous integration, this knowledge point must be mastered
With a monthly salary of 12K, the butterfly changed to a new one and moved forward bravely - she doubled her monthly salary through the career change test~
随机推荐
@Scheduled注解详解
Architecture basic concept and nature of architecture
Work for 5 years, test case design is bad?To look at the big case design summary
FAST-LIO2 code analysis (2)
在MySQL登录时出现Access denied for user ‘root‘@‘localhost‘ (using password YES) 拒绝访问问题解决
Use Jenkins for continuous integration, this knowledge point must be mastered
Docker实践经验:Docker 上部署 mysql8 主从复制
YOLO等目标检测模型的非极大值抑制NMS和评价指标(Acc, Precision, Recall, AP, mAP, RoI)、YOLOv5中[email protected]与
TexturePacker使用文档
contentEditable属性
Building a cloud-native DevOps environment
程序员还差对象?new一个就行了
路径压缩、、
在CDH的hue上的oozie出现,提交 Coordinator My Schedule 时出错
正则表达式
Getting started with IDEA is enough to read this article
在CentOS下安装MySQL
CDH6的Hue打开出现‘ascii‘ codec can‘t encode characters
工件SSMwar exploded 部署工件时出错。请参阅服务器日志了解详细信息
Leetcode 129求根节点到叶节点数字之和、104二叉树的最大深度、8字符串转换整数(atoi)、82删除排序链表中的重复元素II、204二分查找、94二叉树的中序遍历、144二叉树的前序遍历