当前位置:网站首页>Code Casual Recording Notes_Dynamic Programming_322 Change Exchange
Code Casual Recording Notes_Dynamic Programming_322 Change Exchange
2022-08-08 13:44:00 【Erik_Won】
代码随想录笔记_动态规划
代码随想录二刷笔记记录
LC322.零钱兑换
题目
完全背包
给你一个整数数组 coins ,表示不同面额的硬币;以及一个整数 amount ,表示总金额.
计算并返回可以凑成总金额所需的 最少的硬币个数 .如果没有任何一种硬币组合can make up the total amount,返回 -1 .
你可以认为每种硬币的数量是无限的.
示例 1:
输入:coins = [1, 2, 5], amount = 11
输出:3
解释:11 = 5 + 5 + 1
示例 2:
输入:coins = [2], amount = 3
输出:-1
示例 3:
输入:coins = [1], amount = 0
输出:0
思路分析
与 LC 528 不同,This question does not ask for the number of combinations,Instead, find the minimum number of coins,因此,我们可知,This question does not require traversal order.
思路:
1.硬币有无限个
2.Find the minimum number of coins to make up the total amount
从条件1可知,本题是一个完全背包问题.
总金额 = 背包容量,硬币 = 物品, 每种硬币的数量无限:完全背包问题
动态规划五部曲
1.确定dp数组及其下标的含义
dp[j]:can make up the total amount j 的最少硬币个数
即:The minimum items to fill a backpack
2.确定递推公式
能推导出 dp[j] 的上一步为 dp[j - coin[i]]
凑成总金额 j - coin[i] The minimum number of coins is dp[j - coin[i]],此时加上一个coin[i],即 dp[j] = dp[j - coin[i]] + 1
由于 dp[j - coin[i]] 有很多个状态,因此,dp[j] 取所有dp[j - coin[i]] 中最小的.
递推公式:dp[j] = Min(dp[j], dp[j - coin[i]] + 1)
3.初始化
凑成总金额为 0 所需的 coin 个数为 0 ,因此 dp[0] = 0.And because of the characteristic of recursive formula to find the minimum value,Other subscripts should be initialized to the maximum value Integer_Max , 否则,Due to the recursive formula and Min 函数的特点,will be overwritten by the initial value0.
4.遍历顺序
This question can first traverse the backpack or items,没有特别要求
for(int i = 0;i < coins.length(); i++){
// 物品
for(int j = coins[i];j <= amount;j++){
// 背包
dp[j] = Min(dp[j],dp[j - coins[i]] + 1)
}
}
5.推演分析
以 coins[1,2,5] . amount = 5 为例
maxValue = Integer_Max
| 背包容量 | 0 | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|---|
| 物品 0 | 0 | min(maxValue,1) | (maxValue,2) | (maxValue,3) | (maxValue,4) | (mxValue,5) |
| 物品1 | 0 | 1 | (2,1) | (3,2) | (4,2) | (5,3) |
| 物品2 | 0 | 1 | 1 | 2 | 2 | (3,1) |
代码实现
完整代码实现
public static int coinChange(int[] coins, int amount) {
//初始化
int[] dp = new int[amount+1];
for (int i = 0;i < dp.length;i++){
dp[i] = Integer.MAX_VALUE;
}
//总金额为0时,The number of coins is0
dp[0] = 0;
//遍历顺序
for(int i = 0;i < coins.length;i++){
// 遍历物品
for(int j = coins[i];j <= amount;j++){
//遍历背包容量
// If the previous state dp [j - coin[i]] 为最大值,It means that the status has not changed
// So no changes are needed here either
if(dp[j - coins[i]] != Integer.MAX_VALUE)
dp[j] = Math.min(dp[j],dp[j - coins[i]] + 1);
System.out.print(dp[j] + " ");
}
System.out.println();
}
return dp[amount] == Integer.MAX_VALUE ? -1 : dp[amount];
}
边栏推荐
猜你喜欢
![[C language] Dynamic memory management](/img/26/c77a9e8d4481c8ce5b02097cb7c747.png)
[C language] Dynamic memory management

客户案例 | 提高银行信用卡客户贡献率

idea中项目呈现树形结构
![[C language] In-depth analysis of data storage in memory](/img/7c/a277657a85cc0e29db8df919439949.png)
[C language] In-depth analysis of data storage in memory

代码随想录笔记_动态规划_322零钱兑换

连锁小酒馆第一股,海伦司能否梦圆大排档?

Knowledge points and written test questions related to shift operations, bit operations, and logical operations

HackTheBox | Previse

MySQL:锁机制 |表级锁、行级锁 | 排它锁、共享锁 | 间隙锁

《预训练周刊》第56期:长文本理解、即时问答、掩码自监督
随机推荐
连锁小酒馆第一股,海伦司能否梦圆大排档?
bzoj 3624 [Apio2008]免费道路
项目动态|Apache Pulsar 2.10.1 版本介绍
KD-SCFNet:通过知识蒸馏实现更准确、更高效的显着目标检测(ECCV2022)
6. [opencv mouse callback event]
删库不易,跑路更难
Qt操作Sqlite类封装,及命令行导入csv文件到Sqlite数据库
2022-08-05
看到这个应用上下线方式,不禁感叹:优雅,太优雅了!
idea 好工具
干货满满,中科院信工所于静新课帮你get学术研究与论文写作技能
【os.path】的相关用法(持更)
Using Flask and Celery to push real-time/timed messages asynchronously in Win10 environment (Socket.io)/The latest strategy in 2020
难产的“第一股”:中式快餐之困
AfterEffect插件-图层排序-js脚本开发-AE插件
今日睡眠质量记录83分
哈佛大学砸场子:DALL-E 2只是「粘合怪」,生成正确率只有22%
又一个千亿市场,冰淇淋也成了创新试验田
[8月4日]剑指 Offer 52. 两个链表的第一个公共节点
Experience Sharing | Systematic Design and Development of Business Cache