-
Notifications
You must be signed in to change notification settings - Fork 6
/
CoinChangeII.java
38 lines (27 loc) · 1.03 KB
/
CoinChangeII.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
package com.shivaprasad.february.day42;
import java.util.HashMap;
public class CoinChangeII {
public static void main(String[] args) {
System.out.println(change(5,new int[]{1,2,5}));
}
public static int change(int amount, int[] coins) {
return totalWays(coins, 0, amount, new HashMap<String,Integer>());
}
public static int totalWays(int[] coins, int currentIndex, int amount, HashMap<String,Integer> memo){
if(amount==0)
return 1;
if(currentIndex >= coins.length)
return 0;
String currentKey = currentIndex + "_" + amount;
if(memo.containsKey(currentKey)){
return memo.get(currentKey);
}
int consider = 0;
if(coins[currentIndex] <= amount){
consider = totalWays(coins, currentIndex, amount - coins[currentIndex], memo);
}
int notConsider = totalWays(coins, currentIndex + 1, amount, memo);
memo.put(currentKey, consider + notConsider);
return memo.get(currentKey);
}
}