当前位置:网站首页>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];
}
}
边栏推荐
- Department project source code sharing
- color transparency parameter
- The third chapter of the imitation cattle network project: develop the core functions of the community (detailed steps and ideas)
- numpy.hstack
- Dynamic Scene Deblurring with Parameter Selective Sharing and Nested Skip Connections
- Flink学习第四天——完成第一个Flink 流批一体案例
- Sql之各种Join
- OpenCV DNN blogFromImage()详解
- 使用Jenkins做持续集成,这个知识点必须要掌握
- numpy.unique
猜你喜欢

TCP 可靠吗?为什么?
![[LeetCode304 Weekly Competition] Two questions about the base ring tree 6134. Find the closest node to the given two nodes, 6135. The longest cycle in the graph](/img/63/16de443caf28644d79dc6e6889e5dd.png)
[LeetCode304 Weekly Competition] Two questions about the base ring tree 6134. Find the closest node to the given two nodes, 6135. The longest cycle in the graph

Flink学习第三天——一文带你了解什么是Flink流?

Flink Yarn Per Job - Yarn应用

ES中SQL查询详解

【图像融合】基于加权和金字塔实现图像融合附matlab代码

工件SSMwar exploded 部署工件时出错。请参阅服务器日志了解详细信息

GetHashCode与Equals

2022还想上岸学习软件测试必看,测试老鸟的肺腑之言...

DVWA靶场环境搭建
随机推荐
【ACWing】406. 放置机器人
Artifact XXXwar exploded Artifact is being deployed, please wait...(已解决)
UI自动化测试框架搭建-标记性能较差用例
SphereEx苗立尧:云原生架构下的Database Mesh研发实践
Loading configuration of Nacos configuration center
numpy.hstack
OpenCV DNN blogFromImage()详解
【Leetcode】475. Heaters
Excel表格数据导入MySQL数据库
The Spark of Sql join on the and and where
20220725 Information update
工件SSMwar exploded 部署工件时出错。请参阅服务器日志了解详细信息
一款简洁的文件传输工具
Flink学习第五天——Flink可视化控制台依赖配置和界面介绍
Chapter 11 Working with Dates and Times
ELK日志采集
深度学习基础-基于Numpy的循环神经网络(RNN)实现和反向传播训练
DOM 基础操作
【MySQL系列】MySQL数据库基础
mysql8安装make报错如何解决