-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoins.java
105 lines (76 loc) · 1.74 KB
/
Coins.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
public class Coins{
/* Problem: Given an input X and a coin system C,
write a function to determine the minimum number of
coins required to make that exact amount of change.
*/
public static int min(int[] arr){
int minimum = arr[0];
for (int i = 1; i < arr.length; i++){
if (arr[i] < minimum){
minimum = arr[i];
}
}
return minimum;
}
public static int coins(int x, int[] coins){
if (coins == null || x < 0 || coins.length == 0){
throw new IllegalArgumentException("Invalid input");
}
int[] cache = new int[x + 1];
for (int i = 0; i < x + 1; i++){
cache[i] = -1;
}
return coinsHelper(x,coins,cache);
}
/*
public static int coinsHelper(int x, int[] coins, int[] cache){
if (x == 0){
return 0;
}
int min = x;
for (int coin : coins){
if (x - coin >= 0){
int c;
if (cache[x-coin] != -1){
c = cache[x-coin];
}else{
c = coinsHelper(x - coin, coins, cache);
cache[x-coin] = c;
}
if (min > c + 1){
min = c + 1;
}
if (cache[x] == -1){
cache[x] = 1 + coinsHelper(x-coin,coins,cache);
}else{
return cache[x];
}
}
}
return cache[x];
}
*/
public static int coinsHelper(int x, int[] coins, int[] cache){
if (x == 0){
return 0;
}
int min = x;
for (int coin : coins){
if (x - coin >= 0){
int c;
if (cache[x-coin] == -1){
cache[x-coin] = coinsHelper(x - coin, coins, cache);
}
c = cache[x-coin] + 1;
if (c < min){
min = c;
}
}
}
return min;
}
public static void main(String[] args){
int[] c1 = {1,5, 10, 25};
System.out.println(coins(90,c1));
}
}