-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoinChangeProblem.java
57 lines (46 loc) · 1.93 KB
/
CoinChangeProblem.java
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
55
56
57
package com.anudev.ds.dynamicprogramming;
import com.anudev.ds.arrays.ArraysUtility;
/**
* You are given unlimited supply coins of some denominations
* and you have to find a sum. Find the number of ways you can
* achieve using the coins
*/
public class CoinChangeProblem {
private final int sumValue = 8;
private final int[] coinDenominations = {1, 2, 3, 5};
private int[][] memMatrix;
public void findTheSumValue() {
createMemoizationMatrix();
findAndPrintValue();
}
private void createMemoizationMatrix() {
int[] coinValues = new int[coinDenominations.length + 1];
for (int i = 1; i < coinValues.length; i++) {
coinValues[i] = coinDenominations[i - 1];
}
memMatrix = new int[coinValues.length][sumValue + 1];
// putting value for making sum 0 from 0 coin denomination as 1
// number of ways in making the sum 0 with 0 coin is 1: hypothetical
memMatrix[0][0] = 1;
for (int i = 1; i < memMatrix.length; i++) {
for (int j = 0; j < memMatrix[0].length; j++) {
if (coinValues[i] > j) {
memMatrix[i][j] = memMatrix[i - 1][j];
} else {
// case where current denomination coin is included
// : matrix[coins included denomination][sum - current value denomination coin used]
// +
// case where current denomination coin is excluded
// : matrix[coins excluded denomination][current sum required]
memMatrix[i][j] = memMatrix[i][j - coinValues[i]]
+ memMatrix[i - 1][j];
}
}
}
ArraysUtility.printIntMatrix(memMatrix);
}
private void findAndPrintValue() {
System.out.println();
System.out.println("Number of ways are: " + memMatrix[coinDenominations.length][sumValue]);
}
}