-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoinChange.java
More file actions
44 lines (36 loc) · 996 Bytes
/
Copy pathCoinChange.java
File metadata and controls
44 lines (36 loc) · 996 Bytes
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
package DP;
import org.junit.Test;
import java.util.Arrays;
/**
* @author dekai.kong
* @difficult medium
* @create 2020-08-02 12:36
* @from
**/
public class CoinChange {
public CoinChange() {
}
public int coinChange(int[] coins, int amount) {
int[] dp = new int[amount + 1];
Arrays.fill(dp, Integer.MAX_VALUE);
dp[0] = 0;
for (int i = 1; i < dp.length; i++) {
for (int j = 0; j < coins.length; j++) {
if (i >= coins[j] && dp[i - coins[j]] != Integer.MAX_VALUE) {
dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1);
}
}
}
if(dp[amount] == Integer.MAX_VALUE){
dp[amount] = -1;
}
return dp[amount];
}
@Test
public void test() {
coinChange(new int[]{2},3);
// coinChange(new int[]{1,2,5},11);
// coinChange(new int[]{2,5,10,1},27);
// coinChange(new int[]{186,419,83,408},6249);
}
}