当前位置:网站首页>322.零钱兑换
322.零钱兑换
2022-06-22 20:27:00 【zzu菜】
零钱兑换
给你一个整数数组 coins ,表示不同面额的硬币;以及一个整数 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
思考
dp数组含义及其下标意义
dp[j]: 表示使用硬币(其硬币面值小于金额j)凑满金额j的最小硬币数
dp数组初始化
首先我们对coins数组进行排序
这里我们定义
- -1代表不能凑满金额j
- 0 代表amount等于0时,不需要掏出硬币
首先我们初始化dp[0] = 0;
dp[1 …coins[0]-1 ] 赋值为-1,因为在这个总钱数j已经比最小的硬币还小了。
其次初始化 dp[ coins[0] ] =1,因为这个总钱数j正好等于第一个硬币的价格 所以最少需要一个硬币
其余数 = Integer.Maxval;
状态转移方程
dp[j] = dp[j-coins[i]] + 1;
意思是 我们需要求得凑成金额为j的最小硬币数,我们现在有硬币coins[i],所以我们只需要知道凑成
j-coins[i]的最小硬币数即可。
所以当coins[i]≤j时,我们需要选成一个最小的
dp[j] =Min{dp[j-coins[0]] + 1,dp[j-coins[1]] + 1,…,dp[j-coins[i]] + 1}
即可。
public int coinChange(int[] coins, int amount) {
// 排序数组
Arrays.sort(coins);
if(amount==0) return 0;
if(amount<coins[0]) return -1;
// 创建dp数组
int[] dp=new int[amount+1];
// 初始化dp数组
dp[0] = 0;
for (int i=1;i<=coins[0];i++){
if(i==coins[0]) dp[i] = 1;
else dp[i] = -1;
}
for (int i=coins[0]+1;i<=amount;i++){
dp[i] = Integer.MAX_VALUE;
}
// 完善dp数组
for (int i=coins[0]+1;i<=amount;i++){
for (int coin : coins){
if(coin<=i){
// 如果当前物品等于背包容量
if(i==coin) {
dp[i] = 1;
break;
}
// 如果当前 i-coin 背包没法装时,也就是i-coin=-1
if(dp[i-coin]==-1) continue;
int temp = dp[i-coin]+1;
if(temp<dp[i]) dp[i] =temp;
}
}
// 意思是 前面没有一个dp[i-coin]是可用的
if(dp[i]==Integer.MAX_VALUE) dp[i] =-1;
}
for (int i=0;i<=amount;i++){
System.out.println("index="+i+","+dp[i]+ " ");
}
return dp[amount];
}
边栏推荐
- 第026讲:字典:当索引不好用时2 | 课后测试题及答案
- 6-1 二叉搜索树的操作集
- 第022讲:函数:递归是神马 | 课后测试题及答案
- 快速排序模板 & 注意事项
- Database summary: common problems and Optimization in MySQL development
- 第031讲:永久存储:腌制一缸美味的泡菜 | 课后测试题及答案
- RealNetworks vs. Microsoft: the battle in the early streaming media industry
- Oracle数据库中文字符串和英文字符串的截取不同
- 杰理之列免晶振一拖八烧录升级【篇】
- 杰理之开启四声道通话近端变调问题【篇】
猜你喜欢

TC397 Flash
[database] SQL Server quickly creates tables to simulate departments, courses, teachers, students and scores

IDC發布中國數據治理報告 億信華辰第一

TC397 Flash

微软 Edge 浏览器将支持网络测速,内置计算器和单位转换工具

校园跑腿管理端APP—陕西格创

Laravel+ pagoda planning task

第026讲:字典:当索引不好用时2 | 课后测试题及答案

勒索病毒横行下设备该如何进行加密防护

CVPR2022 | 海德堡大学《深度视觉相似性与度量学习》教程
随机推荐
Oracle数据库中文字符串和英文字符串的截取不同
6月25日PMI认证考点防疫要求及考场安排
Lesson 023 and 024: recursion: these little bunnies, Hanoi Tower after class test questions and answers
第028讲:文件:因为懂你,所以永恒 | 课后测试题及答案【无标题】
Jerry's problem of opening the near end of four channel call [chapter]
88- widely circulated parameter optimization, honey or poison?
Based on AI driven macromolecular drug discovery, "Huashen Zhiyao" obtained nearly 500million yuan of round a financing
Lesson 018: function: flexible is powerful after class test questions and answers
Capital and share increase of avita technology under Chang'an is settled: Ningde times will hold about 24%!
(duc/ddc) digital up mixing / quadrature down mixing principle and MATLAB simulation
75- when left join encounters subquery
92 match for several_ Recognize SQL write example
List of outstanding talents: no crystal vibration, one drag, eight burn and upgrade [chapter]
Lesson 025: Dictionary: after class test questions and answers when the index is not easy to use
Lesson 022: function: recursion is god horse after class test questions and answers
第023、024讲:递归:这帮小兔崽子、汉诺塔 | 课后测试题及答案
第021讲:函数:lambda表达式 | 课后测试题及答案
Jerry's near end tone change problem of opening four channel call [chapter]
IDC發布中國數據治理報告 億信華辰第一
IDC发布中国数据治理报告 亿信华辰第一