当前位置:网站首页>322 coin change of leetcode
322 coin change of leetcode
2022-07-27 05:23:00 【Master yuan】
List of articles
1. Description
You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.
Return the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
2. Test Cases
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
3. Solution
3.1 Recursion with memo
class Solution {
public int coinChange(int[] coins, int amount) {
Integer[] dp = new Integer[amount +1];
dp[0] = 0;
find(coins, amount, dp);
return dp[amount];
}
private int find(int[] coins, int amount, Integer[] dp) {
if (amount == 0) return 0;
if(dp[amount] != null )
return dp[amount];
int min =Integer.MAX_VALUE;
for (int c : coins) {
if (amount -c >= 0) {
int r = find(coins, amount-c, dp);
if (r != -1)
min = Math.min(min, r + 1);
}
}
return dp[amount] = (min == Integer.MAX_VALUE ? -1 : min);
}
}
3.2 iterative
class Solution {
public int coinChange(int[] coins, int amount) {
Integer[] dp = new Integer[amount +1];
dp[0] = 0;
for (int i = 1; i <= amount; i++) {
int min = Integer.MAX_VALUE;
for (int c : coins) {
if (i - c >= 0 ) {
int r = dp[i - c];
if (r != -1)
min = Math.min(min, r + 1);
}
}
dp[i] = min == Integer.MAX_VALUE ? -1 : min;
}
return dp[amount];
}
}
4. reflection
It seems that the essence of dynamic planning is exhaustion , Just how to enumerate fewer times , Get results faster .
边栏推荐
- redis锁
- Demo of throttling function -- regular expression matching
- LeetCode之268.Missing number
- B1024 科学计数法
- Interface and abstract class / method learning demo
- Solution and principle analysis of feign call missing request header
- 2021 OWASP top 5: security configuration error
- JVM上篇:内存与垃圾回收篇六--运行时数据区-本地方法&本地方法栈
- JVM Part 1: memory and garbage collection part 3 - runtime data area - overview and threads
- Alphabetic order problem
猜你喜欢

素数筛选(埃氏筛法,区间筛法,欧拉筛法)

JVM上篇:内存与垃圾回收篇十--运行时数据区-直接内存

Prime number screening (Ehrlich sieve method, interval sieve method, Euler sieve method)

How to test the payment process?

34. Analyze flexible.js

34. 分析flexible.js
![[Niuke discussion area] Chapter 7: building safe and efficient enterprise services](/img/62/2b170e8dd5034708aebc6df0bc2b06.png)
[Niuke discussion area] Chapter 7: building safe and efficient enterprise services

接收方设置并发量和限流

redis集群

Use of collection framework
随机推荐
JVM上篇:内存与垃圾回收篇五--运行时数据区-虚拟机栈
Solution and principle analysis of feign call missing request header
知识点总结(一)
Prime number screening (Ehrlich sieve method, interval sieve method, Euler sieve method)
B1027 打印沙漏
简化JDBC的MyBits框架
JVM上篇:内存与垃圾回收篇二--类加载子系统
B1021 个位数统计
JVM上篇:内存与垃圾回收篇十一--执行引擎
The difference between strlen and sizeof
34. Analyze flexible.js
Detailed description of polymorphism
笔记系列之docker安装Postgresql 14
2021 OWASP top 6-10 collection
MQ set expiration time, priority, dead letter queue, delay queue
A math problem cost the chip giant $500million
B1026 program running time
Scientific Computing Library -- Matplotlib
接收方设置并发量和限流
JVM上篇:内存与垃圾回收篇九--运行时数据区-对象的实例化,内存布局与访问定位