forked from AllAlgorithms/java
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinCoinChange.java
More file actions
Latest commit
32 lines (26 loc) · 826 Bytes
/
Copy pathMinCoinChange.java
File metadata and controls
32 lines (26 loc) · 826 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
publicclassMinCoinChange {
staticintminCoins(intcoins[], intm, intV) {
intdp[] = newint[V + 1];
dp[0] = 0;
for (inti = 1; i <= V; i++) {
dp[i] = Integer.MAX_VALUE;
}
for (inti = 1; i <= V; i++) {
for (intj = 0; j < m; j++) {
if (coins[j] <= i) {
intrest = dp[i - coins[j]];
if (rest != Integer.MAX_VALUE && rest + 1 < dp[i]) {
dp[i] = rest + 1;
}
}
}
}
returndp[V];
}
publicstaticvoidmain(Stringargs[]) {
intcoins[] = { 9, 6, 5, 1 };
intm = coins.length;
intvalue = 15;
System.out.println("Minimum coins is " + minCoins(coins, m, value));
}
}