forked from ravipatel447/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountcoin.java
35 lines (33 loc) · 884 Bytes
/
countcoin.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
import java.util.Scanner;
public class countcoin {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
}
public static long count(int coins[], int n, int sum) {
long dp[][] = new long [n+1][sum+1];
for(int i=0;i<n+1;i++){
for(int j=0;j<sum+1;j++){
dp[i][j] = -1;
}
}
return countdp(coins,n,sum,dp);
}
public static long countdp(int coins[], int n, int sum,long dp[][]) {
if(sum==0){
return dp[n][sum]=1;
}
if(sum<0){
return 0;
}
if(n<=0){
return 0;
}
if(dp[n][sum]==-1){
dp[n][sum] = countdp(coins,n-1,sum,dp)+countdp(coins,n,(sum-coins[n-1]),dp);
return dp[n][sum];
}
else{
return dp[n][sum];
}
}
}