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. Spring Boot作为Spring Cloud基础设施

    spring cloud包含的核心特性: Distributed/versioned configuration(分布式配置) Service registration and discovery(服 ...

  2. SeetaFaceQt:Qt多线程

    为什么要做多线程,说个最简单的道理就是我们不希望在软件处理数据的时候界面处于无法响应的假死状态.有些处理是灰常花时间的,如果把这样的处理放到主线程中执行,就会导致软件一条路走到底,要等到处理完才能接收 ...

  3. phpmyadmin拿网站shell

    开门见山 1. 找到一个赌博网站,发现存在php探针界面,在下面输入密码尝试用弱口令进行连接,尝试是否成功 失败的结果是这样. 2. 成功! 3. 连接成功的,点击phpMyAdmin管理,进行弱口令 ...

  4. maven项目从本地向本地仓库导入jar包

    方法一(推荐): <dependency> <groupId>guagua-commons</groupId> <artifactId>guagua-c ...

  5. Emoji表情符号兼容方案

    Emoji表情符号兼容方案 一 什么是Emoji    emoji就是表情符号:词义来自日语(えもじ,e-moji,moji在日语中的含义是字符) 表情符号现已普遍应用于手机短信和网络聊天软件. em ...

  6. php对象:get_object_vars(), get_parent_class(),is_subclass_of(),interface_exists()

    get_object_vars():获得对象的属性,以关联数组形式返回 get_parent_class():获得对象的父类 is_subclass_of():判断对象是否某类(参数2)的子类实例出的 ...

  7. SQL基础教程(第2版)第2章 查询基础:2-2 算数运算符和比较运算符&2-3 逻辑运算符

    ● 包含NULL的运算,其结果也是NULL. ● 判断是否为NULL,需要使用IS NULL或者IS NOT NULL运算符. ■算术运算符 ■需要注意NULL ■比较运算符 这些比较运算符可以对字符 ...

  8. Django1.11序列化与反序列化

    django序列化与反序列化 from rest_framwork import serializers serializers.ModelSerializer 模型类序列化器,必须依据模型类创建序列 ...

  9. NetWork--HTTPS 原理解析<转>

    转载链接:https://www.cnblogs.com/zery/p/5164795.html HTTPS 原理解析   一 前言 在说HTTPS之前先说说什么是HTTP,HTTP就是我们平时浏览网 ...

  10. GNU Autotool介绍

    参考文档: automake(GNU教程) 几句话说清楚17:用Makefile.am和configure.ac构建一个专业的Hello World Creatingamhello-1.0.tar.g ...