当前位置:网站首页>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 <= 3001 <= coins[i] <= 5000coins中的所有值 互不相同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];
}
}
边栏推荐
- 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~
- 【MySQL系列】MySQL索引事务
- 斜堆、、、
- 程序员还差对象?new一个就行了
- 【MySQL篇】初识数据库
- A brief analysis of mobile APP security testing in software testing, shared by a third-party software testing agency in Beijing
- CDH6 Hue to open a "ASCII" codec can 't encode characters
- 工作5年,测试用例都设计不好?来看看大厂的用例设计总结
- 颜色透明参数
- WEB安全基础 - - - XRAY使用
猜你喜欢

Thinkphp 5.0.24变量覆盖漏洞导致RCE分析

在CDH的hue上的oozie出现,提交 Coordinator My Schedule 时出错

2022第六届强网杯部分wp

Share an interface test project (very worth practicing)

Enterprise firewall management, what firewall management tools are there?

oozie startup error on cdh's hue, Cannot allocate containers as requested resource is greater than maximum allowed

Flink Yarn Per Job - CliFrontend

在MySQL登录时出现Access denied for user ‘root‘@‘localhost‘ (using password YES) 拒绝访问问题解决

Chrome书签插件,让你实现高效整理

cmd command
随机推荐
Secondary Vocational Network Security Competition B7 Competition Deployment Process
辛普森悖论
在MySQL中使用MD5加密【入门体验】
Get piggy homestay (short-term rental) data
UI自动化测试框架搭建-标记性能较差用例
如何用Redis实现分布式锁?
Additional Features for Scripting
Quartus 使用 tcl 文件快速配置管脚
ELK日志采集
工件SSMwar exploded 部署工件时出错。请参阅服务器日志了解详细信息
@WebServlet注解(Servlet注解)
async和await用法介绍
Convert LocalDateTime to Date type
The third chapter of the imitation cattle network project: develop the core functions of the community (detailed steps and ideas)
Bean的生命周期
[LeetCode304周赛] 两道关于基环树的题 6134. 找到离给定两个节点最近的节点,6135. 图中的最长环
12306抢票,极限并发带来的思考?
【Leetcode】473. Matchsticks to Square
尚硅谷MySQL学习笔记
2022第六届强网杯部分wp