-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCoinChange.java
More file actions
52 lines (43 loc) · 1.02 KB
/
Copy pathCoinChange.java
File metadata and controls
52 lines (43 loc) · 1.02 KB
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
package DynamicProgramming;
public class CoinChange {
private static int findMinCoins(int S[], int N) {
if (N == 0) {
return 0;
}
if (N < 0) {
return Integer.MAX_VALUE;
}
int coins = Integer.MAX_VALUE;
for (int i = 0; i < S.length; ++i) {
int res = findMinCoins(S, N - S[i]);
if (res != Integer.MAX_VALUE) {
coins = Math.min(coins, res + 1);
}
}
return coins;
}
private static int findMinCoinsIter(int S[], int N) {
int T[] = new int[N + 1];
for (int t = 1; t <= N; ++t) {
T[t] = Integer.MAX_VALUE;
int res = Integer.MAX_VALUE;
for (int s = 0; s < S.length; ++s) {
if (t - S[s] >= 0) {
res = T[t - S[s]];
}
if (res != Integer.MAX_VALUE) {
T[t] = Math.min(T[t], res + 1);
}
}
}
return T[N];
}
public static void main(String[] args) {
int S[] = {1, 3, 5, 7};
int N = 18;
int n = findMinCoins(S, N);
System.out.println(n);
n = findMinCoinsIter(S, N);
System.out.println(n);
}
}