-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_171coinChange.java
More file actions
34 lines (34 loc) · 979 Bytes
/
_171coinChange.java
File metadata and controls
34 lines (34 loc) · 979 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
public class _171coinChange {
public static int coin(int coins[],int sum){
int n = coins.length;
int dp[][]=new int[n+1][sum+1];
for(int i=0;i<=n;i++){
dp[i][0]=1;
}
for(int i=1;i<n+1;i++){
for(int j=1;j<sum+1;j++){
if(coins[i-1]<=j){//valid
dp[i][j]=dp[i][j-coins[i-1]]+dp[i-1][j];
} else{ //invalid
dp[i][j]=dp[i-1][j];
}
}
}
print(dp);
return dp[n][sum];
}
public static void print(int dp[][]){
for(int i = 0;i<dp.length;i++){
for(int j = 0;j<dp[0].length;j++){
System.out.print(dp[i][j]+" ");
}
System.out.println();
}
System.out.println();
}
public static void main(String[] args) {
int coins[]={2,5,3,6};
int sum = 10;
System.out.println(coin(coins, sum));
}
}