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.

Tips:给定一个coins[]数组,表示现有的硬币类型;给定一个整数amount表示要组成的金钱总数。

根据coins[],求出组成amount所需的最少的硬币数。

解法一:循环 :①初始化一个数组dp,并将数组中的值都赋值为Integer.MAX_VALUE;

② 两层循环,第一层遍历amount 第二层遍历coins[].length,当总钱数减去一个硬币的值小于0,证明无法组成该钱数,continue。或者上一步的dp值仍为初始值Integer.MAX_VALUE,也应continue;

都满足条件时,dp选择当前dp值与上一步dp值加一后的最小值,dp[i]=Math.min(dp[i],1+dp[i-coins[j]] );。

③如果无法组成amount这个钱数,就返回-1,否则返回dp[amount].

public int coinChange(int[] coins, int amount) {
int[] dp= new int[amount+1];
for(int i=0;i<=amount;i++){
dp[i]=Integer.MAX_VALUE;
}
dp[0]=0;
for(int i=1;i<=amount;i++){
for(int j=0;j<coins.length;j++){
if(i-coins[j]<0 ||dp[i-coins[j]]==Integer.MAX_VALUE) continue;
dp[i]=Math.min(dp[i],1+dp[i-coins[j]] );
}
}
return dp[amount]==Integer.MAX_VALUE?-1:dp[amount];
}

解法二: 递归 :当前组成amount所需的硬币数,与上一步有关.假设我们已经找到了能组成amount的最少硬币数,那么最后一步,我们可以选择任意的一个硬币,加入这个硬币之前,组成的金钱数为r,这时,r = amount-coins[i](需要循环所有的coins)。依次向前推,直到r等于0或者小于0.

public int coinChange(int[] coins, int amount) {
        if (amount < 0)
            return 0;
        return coinChangeCore(coins, amount, new int[amount]);
    }     private int coinChangeCore(int[] coins, int amount, int[] count) {
        if (amount < 0)
            return -1;
        if (amount == 0)
            return 0;
        //剪枝
        if (count[amount - 1] != 0)
            return count[amount - 1];
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < coins.length; i++) {
            int ans = coinChangeCore(coins, amount - coins[i], count);
            if (ans >= 0 && ans < min) {
                min = 1 + ans;
            }
        }
        count[amount - 1] = (min == Integer.MAX_VALUE) ? -1 : min;
        return count[amount - 1];
    }

【Leetcode】322. Coin Change的更多相关文章

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

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

  2. 【LeetCode】518. Coin Change 2 解题报告(Python)

    [LeetCode]518. Coin Change 2 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目 ...

  3. LeetCode OJ 322. Coin Change DP求解

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

  4. 【LeetCode】860. Lemonade Change 解题报告(Python)

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

  5. 【LeetCode】649. Dota2 Senate 解题报告(Python)

    [LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  6. 【LeetCode】392. Is Subsequence 解题报告(Python)

    [LeetCode]392. Is Subsequence 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/is-subseq ...

  7. 【LeetCode】592. Fraction Addition and Subtraction 解题报告(Python)

    [LeetCode]592. Fraction Addition and Subtraction 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuem ...

  8. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  9. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

随机推荐

  1. 理解同步,异步,阻塞,非阻塞,多路复用,事件驱动IO

    以下是IO的一个基本过程 先理解一下用户空间和内核空间,系统为了保护内核数据,会将寻址空间分为用户空间和内核空间,32位机器为例,高1G字节作为内核空间,低3G字节作为用户空间.当用户程序读取数据的时 ...

  2. 旭日图(sunburst chart)绘制:R语言 & excel

    旭日图(sunburst chart)也叫太阳图,一种圆环镶接图,每一个圆环就代表了同一级别的比例数据,离原点越近的圆环级别越高,最内层的圆表示层次结构的顶级.除了圆环外,旭日图还有若干从原点放射出去 ...

  3. C语言复习20170716

    C语言复习20170716 C数据类型 图片来自:C语言基本数据类型简介 C语言程序处理的数据有常量和变量两种形式. 常量是在程序中不能改变其值的量.例如:整型常量.实型常量.字符常量.字符串常量和枚 ...

  4. C#基础之委托

    委托常常和事件在一起使用,可以理解委托是方法的容器,事件则是委托的另一种表现形式.委托和事件虽然写得还比较多,不过也只是用的熟练而已,趁周末没课好好巩固下基础,一点一点积累吧. 1.一个简单的小例子 ...

  5. cv::Mat转换QImage

    cvtColor(img, img, CV_BGR2RGB); QImage image((uchar*)img.data,img.cols,img.rows,QImage::Format_RGB88 ...

  6. 【转载】C/C++杂记:深入理解数据成员指针、函数成员指针

    原文:C/C++杂记:深入理解数据成员指针.函数成员指针 1. 数据成员指针 对于普通指针变量来说,其值是它所指向的地址,0表示空指针.而对于数据成员指针变量来说,其值是数据成员所在地址相对于对象起始 ...

  7. 【SHOI2008】堵塞的交通

    题面 题解 这里提供几种不用脑子的算法(当然是离线的): $\text{LCT}$ 记下每条边的删除时间,用$\text{LCT}$维护最大生成树,每次加进一条边时,跟原来那条链上的做比较,删除那条删 ...

  8. cf 448c Painting Fence

    http://codeforces.com/problemset/problem/448/C 题目大意:给你一个栅栏,每次选一横排或竖排染色,求把全部染色的最少次数,一个点不能重复染色. 和这道题有点 ...

  9. elasticsearch对某段时间范围内按时间间隔进行统计

    { "query" : { "constant_score" : { "filter" : { "range" : { ...

  10. Introduction to Locking in SQL Server

    Locking is a major part of every RDBMS and is important to know about. It is a database functionalit ...