美文网首页
Coin Change

Coin Change

作者: BLUE_fdf9 | 来源:发表于2018-09-16 05:38 被阅读0次

题目
You are given coins of different denominations and a total amount of money. Write a function to compute the number of combinations that make up that amount. You may assume that you have infinite number of each kind of coin.

Note: You can assume that

0 <= amount <= 5000
1 <= coin <= 5000
the number of coins is less than 500
the answer is guaranteed to fit into signed 32-bit integer

答案

class Solution {
    public int coinChange(int[] coins, int M) {
        // write your code here
        int[] f = new int[M + 1];
        f[0] = 0;
        for(int i = 1; i <= M; i++) {
            f[i] = Integer.MAX_VALUE;
            for(int j = 0; j < coins.length; j++) {
                if(i >= coins[j] && f[i - coins[j]] < Integer.MAX_VALUE) {
                    f[i] = Math.min(f[i], f[i - coins[j]] + 1);
                }
            }
        }
        if(f[M] == Integer.MAX_VALUE) return -1;
        return f[M];
    }
}

相关文章

  • leetcode-12

    Coin Change Boundary: There may be no possible change, so...

  • Coin Change

    题目You are given coins of different denominations and a to...

  • Coin Change

    题目来源一道简单的DP题,n种硬币,要求组成某个数值的硬币数最少,代码如下: 看了下讨论区,感觉可以写的更简洁一下...

  • coin change

    最简单的DP

  • coin change

    You are given coins of different denominations and a tota...

  • 322、Coin Change

    参考 [LeetCode] Coin Change 硬币找零 题目描述:You are given coins o...

  • LeetCode Coin Change

    You are given coins of different denominations and a tota...

  • coin change问题

    最简单的模式,不限定硬币使用的次数! 符合动态规划的要求,最优子问题。即10块的时候最优,必然要求小于10块都是最...

  • Coin Change 2

    题目来源给一个钱数以及一些硬币,硬币可以无限取,问有多少种组合方案。一看就是一道DP题,但是太久没刷题,手生。导致...

  • Coin Change 2

    You are given coins of different denominations and a tota...

网友评论

      本文标题:Coin Change

      本文链接:https://www.haomeiwen.com/subject/yidxnftx.html