LeetCode OJ 322. Coin Change DP求解
题目链接:https://leetcode.com/problems/coin-change/
Submissions: 62250 Difficulty: Medium
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
.
Note:
You may assume that you have an infinite number of each kind of coin.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
Subscribe to see which companies asked this question
给定几个固定面值的硬币,能够无限使用。
一个目标数。要求用最少的硬币兑换这个target。
换一种思路理解题目,每次能够走给定面值的步数。问最少走多少步能达到目标。
如此一来便能够用BFS求解。
另外一种解法是DP,dp[i] = min {dp[i - a], dp[i - b], dp[i - c] ...... }。动态规划仅仅要有公式就能非常快求解。
我用DP求解的AC代码
package march; import java.util.Arrays; /**
* @auther lvsheng
* @date 2016年3月16日
* @time 下午11:20:44
* @project LeetCodeOJ
*
*/ public class CoinChange {
public static void main(String[] args) {
int[] a = { 1, 2, 5 };
System.out.println(coinChange(a, 11));
int[] b = { 2 };
System.out.println(coinChange(b, 3));
} public static int coinChange(int[] coins, int amount) {
int len = coins.length;
if (len == 0) return -1;
int[] dp = new int[amount + 1];
Arrays.fill(dp, Integer.MAX_VALUE);
dp[0] = 0; Arrays.sort(coins); for (int i = 0; i < len && coins[i] <= amount; i++) dp[coins[i]] = 1; for (int i = 1; i <= amount; i++) {
int min = dp[i];
if (min == 1) continue;
for (int j = 0; j < len && coins[j] < i; j++) {
int c = coins[j];
int d = dp[i - c];
if (d != Integer.MAX_VALUE && d < min) min = d + 1;
}
dp[i] = min;
} return dp[amount] == Integer.MAX_VALUE ? -1 : dp[amount];
}
}
LeetCode OJ 322. Coin Change DP求解的更多相关文章
- 【LeetCode】322. Coin Change 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 【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 解题报告(Python)
[LeetCode]518. Coin Change 2 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目 ...
- UVA.674 Coin Change (DP 完全背包)
UVA.674 Coin Change (DP) 题意分析 有5种硬币, 面值分别为1.5.10.25.50,现在给出金额,问可以用多少种方式组成该面值. 每种硬币的数量是无限的.典型完全背包. 状态 ...
- [LeetCode] 322. Coin Change 硬币找零
You are given coins of different denominations and a total amount of money amount. Write a function ...
- 322. Coin Change
动态规划里例题,硬币问题. p[i] = dp[i - coin[j]] + 1; 注意i < coin[j] dp[i-coin[j]]无解都要跳过. public class Solutio ...
- leetcode@ [322] Coin Change (Dynamic Programming)
https://leetcode.com/problems/coin-change/ You are given coins of different denominations and a tota ...
- LeetCode 322. Coin Change
原题 You are given coins of different denominations and a total amount of money amount. Write a functi ...
- dp:322. Coin Change 自下而上的dp
You are given coins of different denominations and a total amount of money amount. Write a function ...
随机推荐
- [转]MapReduce浅析
本文转自http://edisonchou.cnblogs.com/ 一.什么是MapReduce MapReduce是Google的一项重要技术,它首先是一个编程模型,用以进行大数据量的计算.对于大 ...
- spring加载classpath与classpath*的区别别
1.无论是classpath还是classpath*都可以加载整个classpath下(包括jar包里面)的资源文件. 2.classpath只会返回第一个匹配的资源,查找路径是优先在项目中存在资源文 ...
- python学习笔记(3)——进制符号&转换公式
进制转换法则: 进制符号 bin().oct().hex().int('',进制)+待转格式数 10进制→其他进制 # dec2bin # 十进制 to 二进制: bin() >>> ...
- Anniversary Cake
Anniversary Cake Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 15704 Accepted: 5123 ...
- 【转】上传并解析excel
[转载自]http://blog.csdn.net/u011563331/article/details/51322523 通过解析excel,将数据存储到数据库中.现在将方法保存下来. 使用的是ap ...
- JPQL 的基本使用
一.概念 JPQL 语言,即 Java Persistence Query Language 的简称.JPQL 和 HQL 是非常类似的,支持以面向对象的方式来写 SQL 语句,当然也支持本地的 SQ ...
- spring cloud (一):大话 Spring Cloud
转自:http://www.ityouknow.com/springcloud/2017/05/01/simple-springcloud.html 研究了一段时间Spring Boot了准备向Spr ...
- 截取命令cut命令、awk命令、sed命令
cut命令 截取以制表符tab为分隔符的第一列 cut -f 1test.txt 截取以":"为分隔符的第一列,如果比较规律的文件,可以自己设定分隔符 cut -f 1 -d ': ...
- Python进阶-操作redis
1.String 操作 redis中的String在在内存中按照一个name对应一个value来存储 set() #在Redis中设置值,默认不存在则创建,存在则修改 r.set('name', 'z ...
- Batchelor Prize
awards in fluid mechanics The Prize of $25,000 is awarded every four years to a single scientist for ...