LeetCode322. 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.

递归代码(记忆化搜索优化)

public class CoinChange {
    public static int MAX_INT = 100000000;
    public static int[][] result;
    public int search(int idx, int[] coins, int amount) {
        //idx都小于0了,还想拿到amount数目的零钱,只能给你无穷多个
        if (idx < 0) {
            return MAX_INT;
        }
        int a = 0, b = 0;
        //要拿到amount为0的零钱,只能给你0个硬币
        if (amount == 0) {
            return 0;
        }
        //amount都小于0了,也只能给你无穷多个
        if (amount < 0) {
            return MAX_INT;
        }
        if (result[idx][amount] >= 0) {
            return result[idx][amount];
        } 

        a = search(idx, coins, amount - coins[idx]) + 1;    
        b = search(idx - 1, coins, amount);
        result[idx][amount] = Math.min(a, b);
        return result[idx][amount];
    }
    public int coinChange(int[] coins, int amount) {
        result = new int[coins.length][amount+1];
        for (int i = 0; i < coins.length; i++) {
            for (int j = 0; j < amount+1; j++) {
                result[i][j] = -1;
            }
        }
        int res = search(coins.length-1, coins, amount);
        if (res < MAX_INT) {
            return res;
        } else {
            return -1;
        }
    }
}

递归代码优化

上面的写法不太好理解,优化下:

public class Solution {
    public static int MAX_INT = Integer.MAX_VALUE - 1;
    public static int[][] result;
    public int search(int idx, int[] coins, int amount) {
        //idx都小于0了,还想拿到amount数目的零钱,只能给你无穷多个
        if (idx < 0) {
            return MAX_INT;
        }
        int a = MAX_INT, b = MAX_INT;
        //要拿到amount为0的零钱,只能给你0个硬币
        if (amount == 0) {
            return 0;
        }

        if (result[idx][amount] >= 0) {
            return result[idx][amount];
        } 

        if (amount >= coins[idx]) {
            a = search(idx, coins, amount - coins[idx]) + 1;    
        }

        b = search(idx - 1, coins, amount);
        result[idx][amount] = Math.min(a, b);
        return result[idx][amount];
    }
    public int coinChange(int[] coins, int amount) {
        result = new int[coins.length][amount+1];
        for (int i = 0; i < coins.length; i++) {
            for (int j = 0; j < amount+1; j++) {
                result[i][j] = -1;
            }
        }
        int res = search(coins.length-1, coins, amount);
        if (res < MAX_INT) {
            return res;
        } else {
            return -1;
        }
    }
}

递推代码

public int coinChangeDitui(int[] coins, int amount) {
        result[0][0] = 0;
        //对0号硬币的各种情况初始化
        for (int i = 1; i < amount + 1; i++) {
            if (i < coins[0]) {
                //获得的硬币总数i比当前的硬币面值小,没有找零方案
                result[0][i] = MAX_INT;
            } else if (i % coins[0] == 0){
                //可以整除才行
                result[0][i] = i / coins[0];
            } else {
                //否则没有找零方案
                result[0][i] = MAX_INT;
            }
        }
        for (int i = 1; i < coins.length; i++) {
            result[i][0] = 0;
            for (int j = 1; j < amount+1; j++) {
                //不选硬币i
                result[i][j] = result[i-1][j];
                if (j >= coins[i]) {
                    //只有当 获得的硬币总数j比当前硬币的面值大时,才考虑使用硬币i
                    result[i][j] = Math.min(result[i-1][j], result[i][j-coins[i]] + 1);
                }
            }
        }
        if (result[coins.length-1][amount] < MAX_INT) {
            return result[coins.length-1][amount];
        } else {
            return -1;
        }
    }

递推实现优化

使用滚动数组优化。
因为上面其实只用了二维数组相邻的两行数据而已。

    public int coinChangeDituiYouhua(int[] coins, int amount) {

        //这里使用了滚动数组
        result = new int[2][amount+1];
        for (int i = 0; i < coins.length; i++) {
            for (int j = 0; j < amount+1; j++) {
                result[i%2][j] = -1;
            }
        }

        result[0][0] = 0;
        for (int i = 1; i < amount + 1; i++) {
            if (i < coins[0]) {
                result[0][i] = MAX_INT;
            } else if (i % coins[0] == 0){
                result[0][i] = i / coins[0];
            } else {
                result[0][i] = MAX_INT;
            }
        }
        for (int i = 1; i < coins.length; i++) {
            result[i%2][0] = 0;
            for (int j = 1; j < amount+1; j++) {
                result[i%2][j] = result[(i-1)%2][j];
                if (j >= coins[i]) {
                    result[i%2][j] = Math.min(result[(i-1)%2][j], result[i%2][j-coins[i]] + 1);
                }
            }
        }
        if (result[(coins.length-1)%2][amount] < MAX_INT) {
            return result[(coins.length-1)%2][amount];
        } else {
            return -1;
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值