当前位置:网站首页>322. change exchange
322. change exchange
2022-06-22 22:09:00 【Zzu dish】
Change for
Give you an array of integers coins , Coins of different denominations ; And an integer amount , Represents the total amount .
Calculate and return the amount needed to make up the total amount The minimum number of coins . If no combination of coins can make up the total amount , return -1 .
You can think of the number of coins of each kind as infinite .
Example 1:
Input :coins = [1, 2, 5], amount = 11
Output :3
explain :11 = 5 + 5 + 1
Example 2:
Input :coins = [2], amount = 3
Output :-1
Example 3:
Input :coins = [1], amount = 0
Output :0
reflection
dp Array meaning and its subscript meaning
dp[j]: Indicates the use of coins ( The face value of the coin is less than the amount j) Rounding up amount j The minimum number of coins
dp Array initialization
First of all, we are right coins Array to sort
Here we define
- -1 Means you can't make up the whole amount j
- 0 representative amount be equal to 0 when , You don't have to pull out a coin
First we initialize dp[0] = 0;
dp[1 …coins[0]-1 ] The assignment is -1, Because in this total amount of money j It is smaller than the smallest coin .
Secondly, initialize dp[ coins[0] ] =1, Because the total amount of money j Exactly equal to the price of the first coin So at least one coin is needed
Its remainder = Integer.Maxval;
State transition equation
dp[j] = dp[j-coins[i]] + 1;
intend We need to find the rounding up amount as j The minimum number of coins , We have coins now coins[i], So we just need to know how to make up
j-coins[i] The minimum number of coins .
So when coins[i]≤j when , We need to choose the smallest one
dp[j] =Min{dp[j-coins[0]] + 1,dp[j-coins[1]] + 1,…,dp[j-coins[i]] + 1}
that will do .
public int coinChange(int[] coins, int amount) {
// Sort array
Arrays.sort(coins);
if(amount==0) return 0;
if(amount<coins[0]) return -1;
// establish dp Array
int[] dp=new int[amount+1];
// initialization dp Array
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;
}
// perfect dp Array
for (int i=coins[0]+1;i<=amount;i++){
for (int coin : coins){
if(coin<=i){
// If the current item is equal to the backpack capacity
if(i==coin) {
dp[i] = 1;
break;
}
// If at present i-coin When the backpack cannot be loaded , That is to say i-coin=-1
if(dp[i-coin]==-1) continue;
int temp = dp[i-coin]+1;
if(temp<dp[i]) dp[i] =temp;
}
}
// intend There is no one ahead dp[i-coin] Is available
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];
}
边栏推荐
- 2022年6月25日PMP考试通关宝典-6
- 如何在物联网低代码平台中使用数据字典功能?
- IDC發布中國數據治理報告 億信華辰第一
- 二级造价工程师考前必备15个知识点来了!祝你旗开得胜!
- In 2022, the "product innovation and achievement transformation" training camp of Chaoyang District Science and technology innovation class was successfully completed
- Lesson 028: Documents: because I know you, I will never forget the after-school test questions and answers [no title]
- Lesson 020: functions: embedded functions and closures | after class test questions and answers
- 科研热点|官宣!2022年JCR分区和影响因子发布时间确定!
- Lesson 033: exception handling: you can't always be right 2 | after class test questions and answers
- List of outstanding talents: no crystal vibration, one drag, eight burn and upgrade [chapter]
猜你喜欢

Implementation of depth traversal adjacency matrix of figure 6-5

Lesson 030: file system: introduce a big thing | after class test questions and answers

RapidEye快鸟、SPOT卫星遥感影像数据

Implementation of depth traversal adjacency table in Figure 6-7

Lesson 026: Dictionary: when the index is not easy to use 2 | after class test questions and answers

Microsoft edge browser will support network speed measurement, built-in calculator and unit conversion tool
![Jerry's dynamic switching EQ document [chapter]](/img/2d/9a0245b87fb05ea61dbfc7922ea766.png)
Jerry's dynamic switching EQ document [chapter]

Lesson 019: function: my site listen to my after-school test questions and answers

CyCa children's physique etiquette Shenzhen training results assessment successfully concluded

How to use the data dictionary function in the low code platform of the Internet of things?
随机推荐
IDC發布中國數據治理報告 億信華辰第一
Lesson 022: function: recursion is god horse after class test questions and answers
SPA项目开发之登录注册
How to operate redis on the IntelliJ idea database console
第014-15讲:字符串 (见小甲鱼新版27讲-32讲)| 课后测试题及答案
Android kotlin SP DP to PX
Is data scientist a promising profession?
自助图书馆系统-Tkinter界面和openpyxl表格综合设计案例
Lesson 020: functions: embedded functions and closures | after class test questions and answers
Lesson 030: file system: introduce a big thing | after class test questions and answers
【ROS 入门学习 】CmakeList.txt 和Packages.xml释义
Based on AI driven macromolecular drug discovery, "Huashen Zhiyao" obtained nearly 500million yuan of round a financing
Lesson 021: functions: lambda expressions | after class test questions and answers
June 25 PMI certification examination site epidemic prevention requirements and examination room arrangement
sitl_gazebo/include/gazebo_opticalflow_plugin.h:43:18: error: ‘TRUE’ was not declared in this scope
第022讲:函数:递归是神马 | 课后测试题及答案
6月25日PMI认证考点防疫要求及考场安排
Lesson 026: Dictionary: when the index is not easy to use 2 | after class test questions and answers
【持续更新中...】2021年全国大学生电子设计大赛 (三)匿名四轴拓空者飞控系统设计解读
(DUC/DDC)数字上混频/正交下混频原理及matlab仿真