题目链接: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. JavaScript 单例,Hash,抛异常

    1. 单例 ECMA 5 版 记得以前写过几种单例实现,找不到了... function Singleton() { if (this.constructor.instance) { return t ...

  2. list.sort结果是None

    错误原因:  list.sort()功能是针对列表自己内部进行排序, 不会有返回值, 因此返回为None.  举例说明: In [19]: a=["a","c" ...

  3. ES6字符串模板

    这里做个简单的拓展,之前做vue组件时,经常用到拼接字符串,换行时用到\,既费时又麻烦.这里介绍个ES6字符串模板方法 旧版拼接(各种换行拼接) Vue.component('obj-prop',{ ...

  4. BZOJ3124: [Sdoi2013]直径 (树形DP)

    题意:给一颗树 第一问求直径 第二问求有多少条边是所有直径都含有的 题解:求直径就不说了 解第二问需要自己摸索出一些性质 任意记录一条直径后 跑这条直径的每一个点  如果以这个点不经过直径能到达最远的 ...

  5. 代码分析工具splint安装介绍

    官网 http://www.splint.org/ splint能干什么? splint是一个静态检查C语言代码安全弱点和编写错误的开源程序.(不支持C++) splint会进行多种常规检查,包括 空 ...

  6. Android中ProgressDialog自动消失

    主要函数部分代码: final ProgressDialog proDialog = android.app.ProgressDialog .show(MainActivity.this, " ...

  7. The content of element type "resultMap" must match ...

    mybatis中的mapper文件错误 ①错误原因: <resultMap>标签中需要按照一下顺序编写: <id> <result> <association ...

  8. Linux学习笔记(六) 进程管理

    1.进程基础 当输入一个命令时,shell 会同时启动一个进程,这种任务与进程分离的方式是 Linux 系统上重要的概念 每个执行的任务都称为进程,在每个进程启动时,系统都会给它指定一个唯一的 ID, ...

  9. 60.通过应用层join实现用户与博客的关联

    在构造数据模型的时候,将有关联关系的数据分割为不同的实体,类似于关系型数据库中的模型. 案例背景:博客网站,一个网站可能有多个用户,一个用户会发多篇博客,此时最好的方式是建立users和blogs两个 ...

  10. Random和ArrayList的应用

    /*Random类应用与Math类应用,创建一个类, * 1)分别用Random类和Math.random()方法生成随机数. * 2) 把Math.random()方法生成的随机数,转换成1-100 ...