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
32 lines (26 loc) · 826 Bytes
/
MinCoinChange.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
public class MinCoinChange {
static int minCoins(int coins[], int m, int V) {
int dp[] = new int[V + 1];
dp[0] = 0;
for (int i = 1; i <= V; i++) {
dp[i] = Integer.MAX_VALUE;
}
for (int i = 1; i <= V; i++) {
for (int j = 0; j < m; j++) {
if (coins[j] <= i) {
int rest = dp[i - coins[j]];
if (rest != Integer.MAX_VALUE && rest + 1 < dp[i]) {
dp[i] = rest + 1;
}
}
}
}
return dp[V];
}
public static void main(String args[]) {
int coins[] = { 9, 6, 5, 1 };
int m = coins.length;
int value = 15;
System.out.println("Minimum coins is " + minCoins(coins, m, value));
}
}