Skip to content

Straight forward dynamic programming approach #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions Dynamic Programming/The Coin Change Problem/Mayank.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

static long getWays(long n, long[] c){
long[][] dp = new long[c.length][(int)(n+1)];
for(int i=0;i<c.length;i++)
dp[i][0]=1;
for(int i=1;i<=n;i++){
if(i%c[0]==0)
dp[0][i]=1;
else
dp[0][i]=0;
}
for(int i=1;i<c.length;i++){
for(int j=1;j<=n;j++){
if(c[i]>j){
//System.out.println("else if");
dp[i][j]=dp[i-1][j];
}
else{
dp[i][j]=dp[i-1][j]+dp[i][j-(int)c[i]];
}
}
}
System.out.println(dp[c.length-1][(int)n]);
return dp[c.length-1][(int)n];
}

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
long[] c = new long[m];
for(int c_i=0; c_i < m; c_i++){
c[c_i] = in.nextLong();
}
// Print the number of ways of making change for 'n' units using coins having the values given by 'c'
long ways = getWays(n, c);
}
}