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

问题:

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];
    }
}

转载于:https://my.oschina.net/liyurong/blog/1594293

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值