-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoinChange.java
34 lines (31 loc) · 1.13 KB
/
CoinChange.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
import java.util.HashMap;
import java.util.Map;
public class CoinChange {
public int coinChange(int[] coins, int amount, Map<Integer, Integer> memoize) {
if(memoize.containsKey(amount))
return memoize.get(amount);
if(amount == 0)
return 0;
if(amount < 0)
return -1;
int min = -1;
for(int i=0; i<coins.length; i++){
int remainder = amount - coins[i];
int result = coinChange(coins, remainder, memoize);
if(result != -1){
result = result + 1;
min = min == -1 ? result : Math.min(min, result);
}
}
memoize.put(amount, min);
return min;
}
public static void main(String[] args) {
CoinChange coinChange = new CoinChange();
int coins[] = {1,2,5};
int amount = 11;
System.out.println(coinChange.coinChange(coins, amount, new HashMap<>()));
System.out.println(coinChange.coinChange(new int[]{186,419,83,408}, 6249, new HashMap<>()));
System.out.println(coinChange.coinChange(new int[]{1}, 0, new HashMap<>()));
}
}