-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMain322.java
More file actions
54 lines (49 loc) · 1.34 KB
/
Main322.java
File metadata and controls
54 lines (49 loc) · 1.34 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
42
43
44
45
46
47
48
49
50
51
52
53
54
package HOT100;
import java.util.Arrays;
public class Main322 {
public int coinChange(int[] coins, int amount) {
if(amount < 1) {
return 0;
}
return coinChange(coins, amount, new int[amount]);
}
private int coinChange(int[] coins, int amount, int[] count) {
if(amount < 0) {
return -1;
}
if(amount == 0) {
return 0;
}
if(count[amount - 1] != 0) { // 剪枝
return count[amount - 1];
}
int min = Integer.MAX_VALUE;
for (int coin : coins) {
int res = coinChange(coins, amount - coin, count);
if(res >= 0 && res < min) {
min = 1 + res;
}
}
count[amount - 1] = (min == Integer.MAX_VALUE) ? -1 : min;
return count[amount - 1];
}
}
/**
* 动态规划
*/
class Main322_1 {
public int coinChange(int[] coins, int amount) {
int max = amount + 1;
int[] dp = new int[amount + 1];
Arrays.fill(dp, max);
dp[0] = 0;
for (int i = 1; i <= amount; i++) {
for (int j = 0; j < coins.length; j++) {
if(coins[j] <= i) {
dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1);
}
}
}
return dp[amount] > amount ? -1 : dp[amount];
}
}