当前位置:网站首页>LeetCode刷题之322 Coin Change
LeetCode刷题之322 Coin Change
2022-07-27 05:01:00 【圆师傅】
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 带备忘录递归
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. 思考
似乎动态规划的本质就是穷举,只是如何穷举更少的次数,更快的得到结果。
边栏推荐
- Deep Qt5 signal slot new syntax
- Introduction to Kali system ARP (network disconnection sniffing password packet capturing)
- ssm框架整合
- Bean's life cycle & dependency injection * dependency auto assembly
- [optical flow] - data format analysis, flowwarp visualization
- Differences among left join, inner join and right join
- Event
- Li Kou achieved the second largest result
- idea远程调试debug
- Machine learning overview
猜你喜欢

Explore the mysteries of the security, intelligence and performance of the universal altek platform!

The project connects with Alipay payment, and the intranet penetration realizes the monitoring of asynchronous callback notification of successful payment of Alipay

String class

支付流程如何测试?

SSM framework integration

JVM上篇:内存与垃圾回收篇十四--垃圾回收器

Inspiration from "flying man" Jordan! Another "arena" opened by O'Neill
![[acwing] solution to the 61st weekly match](/img/31/765f4ce9f779e8093668e7606e0198.png)
[acwing] solution to the 61st weekly match

Integrate SSM

TypeScript 详解
随机推荐
B1021 个位数统计
Dialog data transfer
Mysql表的约束
How idea creates a groovy project (explain in detail with pictures and texts)
Derivation and explanation of PBR physical illumination calculation formula
实用小工具: Kotlin 代码片段
pyside2____1.安装和案列
辗转相除法
JVM上篇:内存与垃圾回收篇--运行时数据区四-程序计数器
String class
整合SSM
Set static IP for raspberry pie
[untitled] I is circularly accumulated under certain conditions. The condition is usually the length of the loop array. When it exceeds the length, the loop will stop. Because the object cannot judge
Installation and template setting of integrated development environment pychar
Domestic mainstream ERP software market
2021 OWASP top 6-10 collection
Scientific Computing Library - numpy
Acticiti中startProcessInstanceByKey方法在variable表中的如何存储
使用ngrok做内网穿透
[optical flow] - data format analysis, flowwarp visualization