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:

Input: coins = [1, 2, 5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1

Example 2:

Input: coins = [2], amount = 3
Output: -1

Note:
You may assume that you have an infinite number of each kind of coin.

class Solution {
public int coinChange(int[] coins, int amount) {
if (coins == null || coins.length == 0) {
return -1;
}
int[] arr = new int[amount + 1];
for (int i = 1; i <= amount; i++) {
arr[i] = Integer.MAX_VALUE;
for (int j = 0; j < coins.length; j++) {
if (i >= coins[j] && arr[i - coins[j]] != -1) {
// like knapbag exclude the previous amoutn of i - arr[j]
arr[i] = Math.min(arr[i], arr[i - coins[j]] + 1);
}
}
arr[i] = arr[i] == Integer.MAX_VALUE ? -1 : arr[i];
}
return arr[amount];
}
}

[LC] 322. Coin Change的更多相关文章

  1. LeetCode OJ 322. Coin Change DP求解

    题目链接:https://leetcode.com/problems/coin-change/ 322. Coin Change My Submissions Question Total Accep ...

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

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

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

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

  4. 322. Coin Change

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

  5. LeetCode 322. Coin Change

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

  6. 322. Coin Change选取最少的硬币凑整-背包问题变形

    [抄题]: You are given coins of different denominations and a total amount of money amount. Write a fun ...

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

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

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

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

  9. 322. Coin Change零钱兑换

    网址:https://leetcode.com/problems/coin-change/ 典型的动态规划问题,类比背包问题,这就是完全背包问题 问题的阶段:对数值 i 凑硬币 问题的状态:对数值 i ...

随机推荐

  1. H5调微信/支付宝

    (1)微信支付:前端点击按钮==>请求接口(后台的接口,把订单号什么玩意传过去)==>后台自己***去请求微信支付接口(什么微信需要的任何参数和前端无关,都交给后台自己弄吧)==>微 ...

  2. mysql5.6免安装使用

    一.去MYSQL官网下载MYSQL免安装版,由于我的系统是64位的,所以就下载了64位的Mysql版本 http://cdn.mysql.com//Downloads/MySQL-5.6/mysql- ...

  3. Jshint 安装方法

    首先在编辑器中搜索扩展程序 "Jshint" 并安装,安装成功后 打开Javascript文件会出现报错提示: "Failed to load jshint librar ...

  4. 吴裕雄--天生自然 JAVASCRIPT开发学习: 正则表达式

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  5. 斐波那契数列 yield 和list 生成

    def fab_demo4(max): a,n,b = 0,0,1 while n < max: yield b # 生成器走到这一步返回b,需要再次调用才能继续执行 a,b = b,a+b n ...

  6. 洛谷P1257(暴力超时)

    1.先输入再求勾股定理会超时 2.需要一边输入一边求. #include<iostream> #include<cmath>#include<cstdio> usi ...

  7. [极客大挑战 2019]EasySQL

    万能密码直接登陆得到flag admin' or 1=1 #

  8. Thread--currentThread()

    参考:http://bbs.csdn.net/topics/391872079 package thread.demo01; public class MyThread extends Thread ...

  9. 1811 06 pygame 的继续开发

    早上看了  高数和python   好像系统没有保存  桑心啊 关于游戏背景的制作 游戏背景就是    背景在移动  而主人物还在原位置的    常常用于跑酷游戏类  背景开始绘制两张图像  一张完全 ...

  10. [NSConcreteValue doubleValue]: unrecognized selector sent to instance

    今天需求说要给在进入某个页面给某个按钮加上放大效果,心想这还不简单,于是三下五除二的把动画加上提交测试了. 下面是动画的代码 NSTimeInterval time = CACurrentMediaT ...