LeetCode Coin Change
原题链接在这里:https://leetcode.com/problems/coin-change/
题目:
You are given coins of different denominations and a total amount of money amount. Write a function to compute 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.
Example 1:
coins = [1, 2, 5], amount = 11
return 3 (11 = 5 + 5 + 1)
Example 2:
coins = [2], amount = 3
return -1.
题解:
要求满足amount时用的最少coins数目.
Let dp[i] denotes amount == i 时用的最少coins数目. 用一维array储存.
递推时,状态转移dp[i] = Math.min(dp[i], dp[i-coins[j]]+1). dp[i-coins[j]]+1表示用最少个数coin表示i-coins[j].
初始化dp[0] = 0. amount为0时不需要硬币. dp其他值都是Integer的最大值, 之后会每次更新取最小值.
Time Complexity: O(amount * coins.length).
Space: O(amount).
AC Java:
class Solution {
public int coinChange(int[] coins, int amount) {
if(amount == 0){
return 0;
}
if(coins == null || coins.length == 0){
return -1;
}
int [] dp = new int[amount+1];
dp[0] = 0;
for(int i = 1; i<=amount; i++){
dp[i] = Integer.MAX_VALUE;
for(int coin : coins){
if(i-coin<0 || dp[i-coin]==Integer.MAX_VALUE){
continue;
}else{
dp[i] = Math.min(dp[i], dp[i-coin]+1);
}
}
}
return dp[amount] == Integer.MAX_VALUE ? -1 : dp[amount];
}
}
LeetCode Coin Change的更多相关文章
- [LeetCode] Coin Change 硬币找零
You are given coins of different denominations and a total amount of money amount. Write a function ...
- [LeetCode] Coin Change 2 硬币找零之二
You are given coins of different denominations and a total amount of money. Write a function to comp ...
- LeetCode——Coin Change
Question You are given coins of different denominations and a total amount of money amount. Write a ...
- LeetCode OJ 322. Coin Change DP求解
题目链接:https://leetcode.com/problems/coin-change/ 322. Coin Change My Submissions Question Total Accep ...
- [LeetCode] 518. Coin Change 2 硬币找零之二
You are given coins of different denominations and a total amount of money. Write a function to comp ...
- [LeetCode] 322. Coin Change 硬币找零
You are given coins of different denominations and a total amount of money amount. Write a function ...
- [LeetCode] 518. Coin Change 2 硬币找零 2
You are given coins of different denominations and a total amount of money. Write a function to comp ...
- 【LeetCode】518. Coin Change 2 解题报告(Python)
[LeetCode]518. Coin Change 2 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目 ...
- HDOJ 2069 Coin Change(母函数)
Coin Change Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
随机推荐
- js随机数
引子: 在写程序间突然需要用到随机数,于是用到了js的Math.random随机函数,生成指定范围内的随机数,根据网上流传的写法生成指定范围内的随机数如下 function random(min ...
- 配置使用EF常见的一些问题及解决方案
转自http://www.2cto.com/database/201511/449573.html 提示未注册,找不到驱动程序 No Entity Framework provider found f ...
- Linux 第04天
Linux 第04天 1.系统设置工具(网络和打印机)和硬件检测 1.1 setup工具 1.1.1 用户身份验证设置 1.1.2 网络配置 1.1.3 防火墙设置 1.1.4 键盘形式设置 1.1. ...
- [leetcode] 47. Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- NOI 2010 海拔 ——平面图转对偶图
[题目分析] 可以知道,所有的海拔是0或1 最小割转最短路,就可以啦 SPFA被卡,只能换DIJ [代码] #include <cstdio> #include <cstring&g ...
- BZOJ 2460 [BeiJing2011]元素 ——线性基
[题目分析] 线性基,由于最多有63个,只需要排序之后,动态的去维护线性基即可. [代码] #include <cstdio> #include <cstring> #incl ...
- 比管理员(administrator)更高权限的TrustedInstaller
http://www.gezila.com/tutorials/9664.html 什么是TrustedInstaller管理权限 ?好多朋友都在使用Windows7系统.在使用过程中,有些朋友在删除 ...
- Angular过滤器
angular中的过滤器有: currency 过滤货币 number 过滤数字, date 过滤日期 json 把js对象过滤为json字符串 limitTo 截取字符串,参数是正数则从左往右 ...
- JAVA自定义异常
创建自定义异常是为了表示应用程序的一些错误类型,为代码可能发生的一个或多个问题提供新含义. 可区分代码运行时可能出现的相似问题的一个或多个错误,或给出应用程序中一组错误的特定含义. //自定义异常类需 ...
- 01 LabVIEW的类中各个Scope的范围
范例地址: D:\Program Files (x86)\National Instruments\LabVIEW 2015\examples\Object-Oriented Programming\ ...