题目链接: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代码

  1. package march;
  2.  
  3. import java.util.Arrays;
  4.  
  5. /**
  6. * @auther lvsheng
  7. * @date 2016年3月16日
  8. * @time 下午11:20:44
  9. * @project LeetCodeOJ
  10. *
  11. */
  12.  
  13. public class CoinChange {
  14. public static void main(String[] args) {
  15. int[] a = { 1, 2, 5 };
  16. System.out.println(coinChange(a, 11));
  17. int[] b = { 2 };
  18. System.out.println(coinChange(b, 3));
  19. }
  20.  
  21. public static int coinChange(int[] coins, int amount) {
  22. int len = coins.length;
  23. if (len == 0) return -1;
  24. int[] dp = new int[amount + 1];
  25. Arrays.fill(dp, Integer.MAX_VALUE);
  26. dp[0] = 0;
  27.  
  28. Arrays.sort(coins);
  29.  
  30. for (int i = 0; i < len && coins[i] <= amount; i++) dp[coins[i]] = 1;
  31.  
  32. for (int i = 1; i <= amount; i++) {
  33. int min = dp[i];
  34. if (min == 1) continue;
  35. for (int j = 0; j < len && coins[j] < i; j++) {
  36. int c = coins[j];
  37. int d = dp[i - c];
  38. if (d != Integer.MAX_VALUE && d < min) min = d + 1;
  39. }
  40. dp[i] = min;
  41. }
  42.  
  43. return dp[amount] == Integer.MAX_VALUE ? -1 : dp[amount];
  44. }
  45. }

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. 通俗理解LDA主题模型(boss)

    0 前言 看完前面几篇简单的文章后,思路还是不清晰了,但是稍微理解了LDA,下面@Hcy开始详细进入boss篇.其中文章可以分为下述5个步骤: 一个函数:gamma函数 四个分布:二项分布.多项分布. ...

  2. Java 基础入门随笔(9) JavaSE版——文档注释

    上节中写了一些static变量以及静态的方法的定义使用以及与非静态的差别,这节补充下: 如果在一个类中所有方法都为静态的,且无成员变量,这时候需要对对应的类进行限制该类无法创建对象,具体操作如下: p ...

  3. 探索 DWARF 调试格式信息

    https://www.ibm.com/developerworks/cn/aix/library/au-dwarf-debug-format/ 简介 DWARF(使用有属性的记录格式进行调试 )是许 ...

  4. iOS布局进化史

    一.绝对布局.layoutsubviews. 二.父视图相对布局 注意:Autoresizing只能设置父子视图之间的关系,也就是说,Autoresizing只能控制子视图和父视图之间的位置/大小关系 ...

  5. PS切图基本操作

    PS切图基本操作 2016-05-11 20:56:46|  分类: PhotoShop|字号 订阅     下载LOFTER我的照片书  |     1首先在“文件”中打开一张图片.   2点击“移 ...

  6. cookie domain path 跨域

    1.domain表示的是cookie所在的域,默认为请求的地址,如网址为www.jb51.net/test/test.aspx,那么domain默认为www.jb51.net.而跨域访问,如域A为t1 ...

  7. 电子笔记本的思考(1)(ver0.2)

    章节:电子笔记本的思考(1)   陶哲轩在<解题·成长·快乐——陶哲轩教你学数学>中着重强调,用纸笔来“缓存”思维对于数学解题的重要性: 用选定的符号表达你所知道的信息,并画一个示意图.把 ...

  8. Radar Installation POJ - 1328 (贪心)

    题目大意(vj上的翻译版本) 假定海岸线是无限长的直线.陆地位于海岸线的一侧,海洋位于另一侧.每个小岛是位于海洋中的一个点.对于任何一个雷达的安装 (均位于海岸线上),只能覆盖 d 距离,因此海洋中的 ...

  9. Flask项目中整合各组件

    一.介绍 主要介绍flask_sqlalchemy.flask_script.flask_migrate这三个组件该如何整合到flask项目中,以及如何使用. # 安装组件 pip3 install ...

  10. 爬虫实战(二) 用Python爬取网易云歌单

    最近,博主喜欢上了听歌,但是又苦于找不到好音乐,于是就打算到网易云的歌单中逛逛 本着 "用技术改变生活" 的想法,于是便想着写一个爬虫爬取网易云的歌单,并按播放量自动进行排序 这篇 ...