动态规划解决最少硬币凑成m元钱

322. Coin Change

 
  My Submissions
  • Total Accepted: 30668
  • Total Submissions: 120210
  • Difficulty: Medium

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.


public class Solution
{
    /*长度为m的数组c[1...m]中存放一系列子结果,即c[i]为要凑的钱数为i时所需的最少硬币数,则c[m]为所求
  当要找的钱数i(1<i<m)与当前所试探的硬币面值k相等时,结果为1,即c[i]=1
  当i大于当前所试探硬币面值k时,若c[i]为0,即还未赋过值,且c[i-k]不为0,即从i元钱中刨去k元后剩下的钱数可以找开,
             则c[i]=c[i-k]+1
        若c[i]不为0,即已赋过值,则c[i]为c[i-k]+1和c[i]中较小的
*/
    public int coinChange(int[] coins, int amount) 
    {
        for(int i=0;i<coins.length;i++)
        {
            System.out.println(coins[i]);
        }
        int m=amount;
        int n=coins.length;
        int[] a=coins;
        int[] c=new int[m+1];//fu chu zhi 0
        if(amount==0)
            return 0;
       
        for(int i=0;i<n;i++)
        {
            if(a[i]<=m)
            {
                c[a[i]]=1;
            }
        
        }
        for(int i=1;i<=m;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(i>a[j])
                {
                    if(c[i]==0&&c[i-a[j]]!=0)
                    {
                        c[i]=c[i-a[j]]+1;
                        System.out.println("c"+i+":"+c[i]);
                    } 
                    else
                    {
                        if(c[i-a[j]]!=0)
                        {
                            c[i]=c[i-a[j]]+1<c[i]?c[i-a[j]]+1:c[i];
                            System.out.println("c"+i+":"+c[i]);
                        }
                    }
                }
               
            }
        }
        if(c[m]==0)
        return -1;
        else
        return c[m];
        
    }
}

另一种更有效的方法
public int coinChange(int[] coins, int amount) {
    if (amount < 1) return 0;
    int[] dp = new int[amount + 1]; 
    Arrays.fill(dp, Integer.MAX_VALUE);
    dp[0] = 0;
    for (int coin : coins) {
        for (int i = coin; i <= amount; i++) {
            if (dp[i - coin] != Integer.MAX_VALUE) {
                dp[i] = Math.min(dp[i], dp[i - coin] + 1);
            }
        }
    }
    return dp[amount] == Integer.MAX_VALUE ? -1 : dp[amount];
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值