Skip to content

Commit 443a283

Browse files
committed
Coin change
1 parent 99fd383 commit 443a283

File tree

1 file changed

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

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.hellokoding.algorithm;
2+
3+
public class DP_CoinChange {
4+
static int countWays(int[] coins, int targetCoinChange) {
5+
int[] wayOfCoinChanges = new int[targetCoinChange+1];
6+
7+
wayOfCoinChanges[0] = 1;
8+
9+
for (int i = 0; i < coins.length; i++) {
10+
for (int j = coins[i]; j <= targetCoinChange; j++) {
11+
wayOfCoinChanges[j] += wayOfCoinChanges[j - coins[i]];
12+
}
13+
}
14+
15+
return wayOfCoinChanges[targetCoinChange];
16+
}
17+
18+
public static void main(String[] args) {
19+
System.out.println(countWays(new int[]{2, 3, 1}, 4));
20+
}
21+
}

0 commit comments

Comments
 (0)