题目链接:https://leetcode.com/problems/coin-change/

322. Coin Change

My Submissions

Question
Total Accepted: 15289 Total
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

Show Tags
Have you met this question in a real interview?

Yes
No

Discuss

给定几个固定面值的硬币,能够无限使用。

一个目标数。要求用最少的硬币兑换这个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求解的更多相关文章

  1. 【LeetCode】322. Coin Change 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  2. 【Leetcode】322. Coin Change

    You are given coins of different denominations and a total amount of money amount. Write a function ...

  3. 【LeetCode】518. Coin Change 2 解题报告(Python)

    [LeetCode]518. Coin Change 2 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目 ...

  4. UVA.674 Coin Change (DP 完全背包)

    UVA.674 Coin Change (DP) 题意分析 有5种硬币, 面值分别为1.5.10.25.50,现在给出金额,问可以用多少种方式组成该面值. 每种硬币的数量是无限的.典型完全背包. 状态 ...

  5. [LeetCode] 322. Coin Change 硬币找零

    You are given coins of different denominations and a total amount of money amount. Write a function ...

  6. 322. Coin Change

    动态规划里例题,硬币问题. p[i] = dp[i - coin[j]] + 1; 注意i < coin[j] dp[i-coin[j]]无解都要跳过. public class Solutio ...

  7. leetcode@ [322] Coin Change (Dynamic Programming)

    https://leetcode.com/problems/coin-change/ You are given coins of different denominations and a tota ...

  8. LeetCode 322. Coin Change

    原题 You are given coins of different denominations and a total amount of money amount. Write a functi ...

  9. dp:322. Coin Change 自下而上的dp

    You are given coins of different denominations and a total amount of money amount. Write a function ...

随机推荐

  1. JS高级——监听浏览器的返回事件

    https://www.cnblogs.com/Easty/p/7820055.html https://www.cnblogs.com/zhengyan/p/6912526.html http:// ...

  2. 关于联想笔记本不能连接无线网(wifi),注销后重新登录才可以连接

    解决联想笔记本wifi问题(果果) 最近很多使用联想的朋友都遇到了这样一个问题,那就是笔记本的wifi突然不能用了,好吧,其实我个人也遇到了这个问题,但是网上貌似对这个问题并没有给出一个可以解决的办法 ...

  3. Markdown(github)语法

    << 访问 Wow!Ubuntu NOTE: This is Simplelified Chinese Edition Document of Markdown Syntax. If yo ...

  4. public private protected

    初学C++的朋友经常在类中看到public,protected,private以及它们在继承中表示的一些访问范围,很容易搞糊涂.今天本文就来十分分析一下C++中public.protected及pri ...

  5. 散列--P1047 校门外的树

    题目描述 某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是1米.我们可以把马路看成一个数轴,马路的一端在数轴0的位置,另一端在L的位置:数轴上的每个整数点,即0,1,2,-,L,都种有 ...

  6. Navicat for MySQL(Ubuntu)过期解决方法

    推荐购买正版软件,尊重版权  [官网在这里] Navicat for MySQL(Ubuntu系统)免费版试用过期解决方法: Step1. 直接删除 /home目录下的  .navicat文件夹(64 ...

  7. vim基础(一)

    今天看了下兄弟连的VIM讲解,又学了几个新命令,记录一下. 插入与删除 插入 首先还是插入,以前只知道i.今天发现原来还有a\A\i\I\o\O,下面具体说一下: 命令 含义 a 在光标后插入 A 在 ...

  8. Python supprocess模块

    当我们需要调用系统的命令的时候,最先考虑的os模块.用os.system()和os.popen()来进行操作.但是这两个命令过于简单,不能完成一些复杂的操作,如给运行的命令提供输入或者读取命令的输出, ...

  9. led1,1s取反,led2计数10次取反

    1 //利用定时器0 1s,led1取反,利用计数器1,跳10,取反 #include<reg52.h> #define uchar unsigned char #define uint ...

  10. PM2 & chmod +x

    PM2 https://www.npmjs.com/package/pm2 https://github.com/Unitech/pm2 docs https://pm2.io/doc/en/runt ...