数组中的数组成一个给定值所需要的最小个数 Coin Change

编程入门 行业动态 更新时间:2024-10-06 16:29:43

数组中的数组成一个给定值<a href=https://www.elefans.com/category/jswz/34/1742150.html style=所需要的最小个数 Coin Change"/>

数组中的数组成一个给定值所需要的最小个数 Coin Change

2019独角兽企业重金招聘Python工程师标准>>>

问题:

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.

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

解决:

① dfs方式,超时。

class Solution {
    int count = Integer.MAX_VALUE;
    public int coinChange(int[] coins, int amount) {
        dfs(coins,amount,coins.length - 1,0);
        if (count == Integer.MAX_VALUE){
            return -1;
        }else {
            return count;
        }
    }
    public void dfs(int[] coins,int amount,int i,int num) {
        if (i < 0) return;
        if (amount == 0){
            if (num < count){//小于最小次数,更新
                count = num;
            }
        }else{
            if (num > count) return;//大于最小次数不再递归
            for (int j = i;j >= 0;j --){
                if (amount >= coins[j]){
                    dfs(coins,amount - coins[j],j,num + 1);
                }
            }
        }
    }
}

② 动态规划。每个数的个数是无穷的,完全背包。

设dp[i][j]表示使用前 i 种硬币凑成 j 元使用的最少硬币个数。

 dp[i][j] = Math.min(dp[i][j], dp[i][j - coins[i]] + 1); (j >= coins[i]) 

coins[i]为第i种硬币,j - coins[i]表示减去第i个硬币的值,剩余的钱数在dp数组中找到值,然后加1和当前dp数组中的值做比较,取较小的那个更新dp数组。

由于i对状态转移方程的影响不大,状态转移方程可以转换为:

dp[i] = min(dp[i], dp[i - coins[j]] + 1); dp[i]表示钱数为 i 时的最小硬币数。其中coins[j]为第j个硬币。

 class Solution { //22ms
    public int coinChange(int[] coins, int amount) {
        if (amount == 0) return 0;
        int[] dp = new int[amount + 1];
        dp[0] = 0;
        for (int i = 0;i <= amount;i ++){//初始化
            dp[i] = Integer.MAX_VALUE - 1;//避免下面溢出
        }
        for (int i = 0;i < coins.length;i ++){//更新dp
            if (coins[i] <= amount){
                dp[coins[i]] = 1;
            }
        }
        for (int i = 0;i < coins.length;i ++){
            for (int j = 0;j <= amount;j ++){
                if (j >= coins[i] && dp[j - coins[i]] >= 0){
                    dp[j] = Math.min(dp[j],dp[j - coins[i]] + 1);
                }
            }
        }
        if (dp[amount] >= Integer.MAX_VALUE - 1) return -1;
        else{
            return dp[amount];
        }
    }
}

【进化版】

class Solution {//13ms
    public int coinChange(int[] coins, int amount) {
        int[] dp = new int[amount + 1];
        for (int i = 1; i < dp.length; i++){
            dp[i] = amount + 1;
        }
        for (int coin : coins){
            for (int j = coin; j <= amount; j ++){
                dp[j] = Math.min(dp[j], dp[j - coin] + 1);
            }
        }
        return dp[amount] > amount? -1 : dp[amount];
    }
}

② 另一种动态规划的思路:

设dp [i]为获得金额i所需的最小硬币数量。

如果dp[i]可达:dp[i+a_coin] = min(dp[i+a_coin], dp[i]+1) 
如果dp[i]不可达:dp[i+a_coin] = dp[i+a_coin] 

dp[i]初始化为最大整数值。

 class Solution{ //25ms
    public int coinChange(int[] coins, int amount) {
        if (amount == 0) return 0;
        int[] dp = new int[amount + 1];
        dp[0] = 0;
        for (int i = 1;i <= amount;i ++){
            dp[i] = Integer.MAX_VALUE;
        }
        for (int i = 0;i <= amount;i ++){
            for (int coin : coins){
                if (coin != Integer.MAX_VALUE && i + coin <= amount && dp[i] != Integer.MAX_VALUE){
                    dp[i + coin] = Math.min(dp[i + coin],dp[i] + 1);
                }
            }
        }
        if (dp[amount] >= Integer.MAX_VALUE){
            return -1;
        }
        return dp[amount];
    }
}

转载于:

更多推荐

数组中的数组成一个给定值所需要的最小个数 Coin Change

本文发布于:2024-03-12 13:19:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1731605.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:所需要   组中   个数   最小   定值

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!