-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoin_Change_Count_Ways.java
More file actions
37 lines (35 loc) · 933 Bytes
/
Coin_Change_Count_Ways.java
File metadata and controls
37 lines (35 loc) · 933 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
33
34
35
36
37
import java.util.*;
public class Coin_Change_Count_Ways {
class Solution {
public long count(int coins[], int N, int sum) {
long[][] dp = new long[N+1][sum+1];
for(long[] arr : dp)
{
Arrays.fill(arr , -1);
}
return fun(coins , N , sum , dp);
}
private long fun(int[] coins , int n , int sum , long[][] dp)
{
if(sum == 0)
{
return 1;
}
if(sum<0)
{
return 0;
}
if(n<=0 && sum>0)
{
return 0;
}
if(dp[n][sum] != -1)
{
return dp[n][sum];
}
long inc = fun(coins , n , sum - coins[n-1] , dp);
long exc = fun(coins , n-1 , sum , dp);
return dp[n][sum] = inc + exc;
}
}
}