当前位置:网站首页>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];
}
边栏推荐
猜你喜欢
【黑马早报】巴菲特罕见巨亏近3000亿;周鸿祎回应360不能卸载;三亚倡议酒店不变相提高房价;首个国产抗新冠口服药定价不超300元...
“自降估值”3个亿的咖啡独角兽要IPO了
Knowledge points and written test questions related to shift operations, bit operations, and logical operations
你是什么时候对深度学习失去信心的?
KD-SCFNet: More Accurate and Efficient Salient Object Detection Through Knowledge Distillation (ECCV2022)
【软考 系统架构设计师】软件架构设计⑥ 软件产品线
QtWebassembly遇到的一些报错问题及解决方案
【os.path】的相关用法(持更)
用 Antlr 重构脚本解释器
C language small project - complete code of minesweeper game (recursive expansion + selection mark)
随机推荐
a += 1 += 1为什么是错的?
Harvard University smashes the field: DALL-E 2 is just a "glue monster", and the generation accuracy rate is only 22%
Qt的简易日志库实现及封装
医药行业转型发展,探索数字化供应链升级之道
HackTheBox | Horizontall
Qt操作Sqlite类封装,及命令行导入csv文件到Sqlite数据库
Prometheus监控Harbor(二进制版)
华谊“在劫难逃”,4年亏掉64亿
2022-08-04
译文推荐|深入解析 BookKeeper 协议模型与验证
webgl 基础
【低代码】1405- 浅谈低代码平台远程组件加载方案
serialize serialize native method
UnsatisfiedDependencyException: Error creating bean with name ‘
R语言数据类型转换:基本数据类型的转换、将一种数据类型转化为另外一种数据类型
【黑马早报】巴菲特罕见巨亏近3000亿;周鸿祎回应360不能卸载;三亚倡议酒店不变相提高房价;首个国产抗新冠口服药定价不超300元...
The programmer essential VS debugging technique
教学习编程,第一步解决自信问题,培养自己的专注力
代码随想录笔记_动态规划_322零钱兑换
php文件上传下载(存放文件二进制到数据库)