Question: Need help with coin change problem - Dynamic Programming technique. It is producing wrong output. package assn; public class cc { public static int min(int

Need help with coin change problem - Dynamic Programming technique.

It is producing wrong output.

package assn;

public class cc {

public static int min(int a, int b) {

if (a > b) { return b; }

else { return a; }

}

public static int minCoin(int n, int m, int wt[], int memo[]) {

if (memo[n] != -1) { return memo[n];

}

if (m == 0) {

return 0; }

if (n == 0) {

return m;

}

if (m < wt[n]) {

return minCoin(n - 1, m, wt, memo);

}

else { memo[n] = min(minCoin(n - 1, m, wt, memo), 1 + minCoin(n, m - wt[n], wt, memo));

return memo[n];

} }

public static void main(String[] args) { // TODO Auto-generated method stub // code

long startTime = System.nanoTime(); int n = 5; int m = 227; int wt[] = new int[6]; wt[0] = 1; wt[1] = 2; wt[2] = 5; wt[3] = 10; wt[4] = 25; wt[5] = 75;

int memo[] = new int[7];

for (int i = 0; i < 6; i++) { memo[i] = -1; }

int ans = minCoin(n, m, wt, memo);

System.out.println("" + ans);

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!