Skip to content

Commit 4427c46

Browse files
committed
Coin change
1 parent da9a4f6 commit 4427c46

File tree

1 file changed

+26
-0
lines changed
  • datastructure-algorithm-java-examples/src/main/java/com/hellokoding/algorithm

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.hellokoding.algorithm;
2+
3+
import java.util.Arrays;
4+
5+
public class DP_CoinChange2 {
6+
static int countMin(int[] coins, int targetCoinChange) {
7+
int[] minNoOfCoins = new int[targetCoinChange+1];
8+
Arrays.fill(minNoOfCoins, targetCoinChange + 1);
9+
10+
minNoOfCoins[0] = 0;
11+
12+
for (int i = 1; i <= targetCoinChange; i++) {
13+
for (int j = 0; j < coins.length; j++) {
14+
if (coins[j] <= i) {
15+
minNoOfCoins[i] = Math.min(minNoOfCoins[i], minNoOfCoins[i - coins[j]] + 1);
16+
}
17+
}
18+
}
19+
20+
return minNoOfCoins[targetCoinChange] > targetCoinChange ? -1 : minNoOfCoins[targetCoinChange];
21+
}
22+
23+
public static void main(String[] args) {
24+
System.out.println(countMin(new int[]{2, 3, 1}, 3));
25+
}
26+
}

0 commit comments

Comments
 (0)