当前位置:网站首页>Cash Ⅱ LeetCode_518_ change
Cash Ⅱ LeetCode_518_ change
2022-08-02 00:07: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]:数量为jnumber of combinations
- 确定递推公式
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];
}
}
边栏推荐
猜你喜欢

Flink Yarn Per Job - Yarn应用

工作5年,测试用例都设计不好?来看看大厂的用例设计总结

在MySQL中使用MD5加密【入门体验】

Quartus 使用 tcl 文件快速配置管脚

DVWA靶场环境搭建

Share an interface test project (very worth practicing)

使用 Zadig 交付云原生微服务应用

Excel表格数据导入MySQL数据库

A brief analysis of mobile APP security testing in software testing, shared by a third-party software testing agency in Beijing

【图像融合】基于加权和金字塔实现图像融合附matlab代码
随机推荐
color transparency parameter
DOM 事件及事件委托
Share an interface test project (very worth practicing)
Quartus uses tcl files to quickly configure pins
Programmer is still short of objects? A new one is enough
Bean的生命周期
Docker搭建Mysql主从复制
20220725 Information update
Dynamic Scene Deblurring with Parameter Selective Sharing and Nested Skip Connections
Flink Yarn Per Job - CliFrontend
cdh6打开oozieWeb页面,Oozie web console is disabled.
Loading configuration of Nacos configuration center
numpy.hstack
Convert LocalDateTime to Date type
Sql之各种Join
Docker实践经验:Docker 上部署 mysql8 主从复制
@Transactional注解在类上还是接口上使用,哪种方式更好?
Use Jenkins for continuous integration, this knowledge point must be mastered
检查 Oracle 版本的 7 种方法
1个月写900多条用例,二线城市年薪33W+的测试经理能有多卷?