leetcode 322. 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.

给出两种解法
第一种解法会超时
主要是dp[i][j] 表示用前i种硬币,生成j

    public int coinChange_bad(int[] coins, int amount) {
        if(coins==null || coins.length==0) return -1;
        int[][] dp = new int[coins.length+1][amount+1];
        int t,k;
        Arrays.fill(dp[0], -1);
        for(int i=0; i< coins.length; i++)
            dp[i][0] = 0;
        for(int i=1; i < coins.length +1; i++){
            // Arrays.fill(dp[i], -1);
            for(int j=1; j < amount+1; j++){
                dp[i][j] = dp[i-1][j] >0 ? dp[i-1][j] : Integer.MAX_VALUE;
                t = j - coins[i-1];
                k=1;
                while(t>=0 ){
                    if(dp[i][t] >= 0)
                        dp[i][j] = Math.min(dp[i][j], dp[i][t] + k);
                    t -= coins[i-1];
                    k++;
                }
                if(dp[i][j] == Integer.MAX_VALUE) 
                    dp[i][j] = -1;
            }
        }
        // for(int i=0; i< coins.length+1; i++)
        //     System.out.println(Arrays.toString(dp[i]));
        return dp[coins.length][amount];
    }

第二种解法,终于AC了
主要是减少无谓的判断
dp[i]表示用coins集合组合成i的最少个数,不能为-1.
所以只需判断
dp[i- k*coins[j]]就可以,取最小,k从小到大,能找到,就停止。

    public int coinChange(int[] coins, int amount) {
        if(coins==null || coins.length==0) return -1;
        int[] dp = new int[amount+1];
        Arrays.fill(dp, -1);
        dp[0] = 0;
        Arrays.sort(coins);
        for(int i=0; i<coins.length; i++){
            if(coins[i] < amount){
                dp[coins[i]] = 1;
            }else{
                break;
            }
        }
        for(int j = 1; j < amount+1; j++){
            if(dp[j] > 0 || j < coins[0]) continue;
            int min = Integer.MAX_VALUE;
            int k=1;
            while(j - coins[0]*k >=0 ){
                min = Integer.MAX_VALUE;
                for(int i=0; i< coins.length; i++){
                    int t = j-k*coins[i];
                    if(t < 0) break;
                    if(dp[t] >= 0){
                        min = Math.min(min, dp[t]+k);
                    }
                }
                if(min < Integer.MAX_VALUE)
                    break;
                k++;
            }
            if(j - coins[0]*k <0 )
                dp[j] = -1;
            else
                dp[j] = min;
        }
        return dp[amount];
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值