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. 安装postgresql后找不到服务 postgresql service

    问题再现 环境: postgresql: 11.5 windows 10 企业版LTSC 64位 使用postgresql-11.5-1-windows-x64.exe安装后,让重新启动,但是重启后, ...

  2. javascript语法规范和良好的变成习惯

    1.1空白和多行书写 1.空白:空格键输入的空白.tab键输入的空白以及回车键输入的空白 2.多行书写,不能将引号内的字符串放到两行,不然容易报错. 1.2点语法 . 点语法表达式由对象开始,接着是一 ...

  3. POJ - 1127 Jack Straws(几何)

    题意:桌子上放着n根木棍,已知木棍两端的坐标.给定几对木棍,判断每对木棍是否相连.当两根木棍之间有公共点或可以通过相连的木棍间接的连在一起,则认为是相连的. 分析: 1.若线段i与j平行,且有部分重合 ...

  4. Python String startswith() Method

    一,摘自官方API  https://docs.python.org/3/library/stdtypes.html#methods str.startswith(prefix[, start[, e ...

  5. java 简单的冒泡

    import java.util.Arrays; public class mao { public static void main(String[] args) { int [] array={1 ...

  6. 使用Python绘制新型冠状肺炎全国增长趋势图

    截至1月28日24时,国家卫生健康委收到31个省(区.市)累计报告确诊病例5974例,现有重症病例1239例,累计死亡病例132例,累计治愈出院103例.现有疑似病例9239例.目前累计追踪到密切接触 ...

  7. HTML5 Canvas——基础入门

    认识canvas html5的新标签 <canvas>标签只是图像容器,必须使用js来绘制图形 可以通过多种方法使用canvas绘制路径,盒,圆,字符以及添加图像 canvas画布 < ...

  8. B. Odd Sum Segments CF(分割数组)

    题目地址 http://codeforces.com/contest/1196/problem/B B. Odd Sum Segments time limit per test 3 seconds ...

  9. linux tar/ tar.gz文件解压

    1.tar 压缩 tar -cvf jpg.tar *.jpg //将目录里所有jpg文件打包成tar.jpg tar -czf jpg.tar.gz *.jpg   //将目录里所有jpg文件打包成 ...

  10. Linux-线程引入

    1.使用进程技术的优势 (1).CPU分时复用,单核心CPU可以实现宏观上的并行 (2).实现多任务系统需求(多任务的系统是客观的) 2.进程技术的劣势 (1).进程间切换开销大 (2).进程间通信麻 ...