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 ...
随机推荐
- servlet——web应用中路径问题
target.html <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html ...
- 数据库–Cobar分布式数据库集群MySQL中间件
运行环境: 主机1:Ubuntu14.04 Desktop + MySQL5.5 + JDK 1.7(HP Z400) 内网IP地址:192.168.137.8 NODE1:Ubuntu 13.04 ...
- codeforces_459D_(线段树,离散化,求逆序数)
链接:http://codeforces.com/problemset/problem/459/D D. Pashmak and Parmida's problem time limit per te ...
- Python自学-2-python解释器
写python源文件,以.py为后缀名 用python解释器去执行.py文件 python解释器 CPython:官方版本,由C语言开发的,下载默认就是这个,使用最广的解释器. 用>> ...
- 微信支付开发 c#
代码demo下载地址: https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=11_1
- 支持向量机(SVM)原理浅析
因为网页博客输入公式很麻烦,所以就在word上面写了,然后截图发上来. 后续关于SVM和FC在深度学习当中得使用对比分析,我再补充.
- UVA - 11536 Smallest Sub-Array(尺取法)
题目: 思路: 读完题之后第一时间想到的是尺取法来做这个题,结果让自己写写崩了,还是练得少!! 到网上搜了一下学习了大佬的标记方法,用一个变量来判断是不是都已经出现,要比每次都判断一下快超多. 代码: ...
- Packages on Ubuntu OS
openSSH https://help.ubuntu.com/lts/serverguide/openssh-server.html
- angular中多个promise的合并处理
all()方法 这个all()方法,可以把多个primise的数组合并成一个.当所有的promise执行成功后,会执行后面的回调.回调中的参数,是每个promise执行的结果.当批量的执行某些方法时, ...
- 【17】AngularJS Bootstrap
AngularJS Bootstrap AngularJS 的首选样式表是 Twitter Bootstrap, Twitter Bootstrap 是目前最受欢迎的前端框架. Bootstrap 你 ...