java找零钱_LeetCode 322. Coin Change找零钱 动态规划 java

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.

给你一些硬币,要你求出通过组合这些硬币,来凑到题目给你的钱的数量(词穷,实在不会翻译)。return硬币数量最少的情况。如果不能通过这些硬币来凑到这个钱,return -1。

首先我们看到fewest number of coins,就要知道这是一个dp的题目,因为随着钱的面额的增大,我们都是要从之前的情况(最少硬币)下手。也就是说,这道题的最终答案跟每一个之前的答案都是有关系的。所以是dp。

第一,创建一个dp数组,应该是比amount的大小更大一个。常规操作。

第二,把dp数组每个元素初始化为amount + 1,这样可以用于之后的判定,看看从这些给的硬币中可不可以凑齐想要的钱。

第三,对于if(coins[j] <= i) 这个if statement在检查题目给我们的硬币的大小是不是比我们遍历dp的钱的数量小。譬如我们遍历DP[1]的时候:这个时候我们检查的是最少多少个硬币可以凑齐一块钱,又假如我们的coins最小是两块钱,这个时候我们就不可以对dp[1]的数据进行更改,会越界。所以我们应该跳过dp[1],直接开始研究下一个情况。

第四, dp[i - coins[j]] + 1这个意思是,在i块钱的时候,我们找那些比i块钱小的硬币,看看再加一个哪一种硬币可以凑齐i块钱。Math.min就在帮我们追踪最小值。

第五,在return的时候我们要注意检查。如果我们最终dp[amount]的钱比amount还要大,就证明我们无法用这些硬币凑齐这个amount。如果小于等于,则可以。

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

if(coins.length == 0) return 0;

int dp [] = new int [amount + 1];

Arrays.fill(dp, amount + 1);

dp[0] = 0; //Initial setting for 0 dollar

for(int i = 1; i < dp.length; i++){

for(int j = 0; j < coins.length; j++){

//check if the value of each coin is less than amount of money in dp

if(coins[j] <= i){

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

}

}

}

return dp[amount] > amount ? -1 : dp[amount];

}

Reflection: Really similar to climbing stairs. Always think how the final result relates to the steps. There is also another approach for this problem: dfs.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值