-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCoinChange.java
More file actions
64 lines (39 loc) · 1.3 KB
/
CoinChange.java
File metadata and controls
64 lines (39 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package company;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
/**
* @Author: Wenhang Chen
* @Description:给定不同面额的硬币 coins 和一个总金额 amount。编写一个函数来计算可以凑成总金额所需的最少的硬币个数。如果没有任何一种硬币组合能组成总金额,返回 -1。
* <p>
* 示例 1:
* <p>
* 输入: coins = [1, 2, 5], amount = 11
* 输出: 3
* 解释: 11 = 5 + 5 + 1
* 示例 2:
* <p>
* 输入: coins = [2], amount = 3
* 输出: -1
* @Date: Created in 19:11 2/18/2020
* @Modified by:
*/
public class CoinChange {
public int coinChange(int[] coins, int amount) {
// 动态规划
// dp[i] = x 表示,当目标金额为 i 时,至少需要 x 枚硬币。
int[] dp = new int[amount + 1];
// 相当于初始化为正无穷,便于后续取最小值
Arrays.fill(dp, amount + 1);
dp[0] = 0;
for (int i = 0; i < dp.length; i++) {
for (int coin : coins) {
// 子问题无解,跳过
if (i - coin < 0) continue;
dp[i] = Math.min(dp[i], 1 + dp[i - coin]);
}
}
return (dp[amount] == amount + 1) ? -1 : dp[amount];
}
}