Coin change using dynamic programming

安稳与你 提交于 2021-01-21 09:50:52

问题


I've been working on coin change problem using dynamic programming. I've tried to make an array fin[] which contains the minimum number of coins required for that index and then print it. I've written a code which I think should give correct output but I can't figure out why it is not giving exact answer. For eg: for the input: 4 3 1 2 3 (4 is the amount to change, 3 the number of types of available coins, 1 2 3 is the list of coin values) The output should be: 0 1 1 1 2 (as we have 1,2,3 as available coins, it requires 0 coins to change 0, 1 coin to change 1, 1 coin to change 2, 1 coin to change 3 and 2 coins to change 4) but it is giving 0 1 2 2 2

here's the code:

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in= new Scanner(System.in);
        int ch = in.nextInt();
        int noc = in.nextInt();
        int[] ca = new int[noc];
        for(int i=0;i<noc;i++)
            {
                //taking input for coins available say a,b,c
            ca[i] = in.nextInt();
        }

       int[] fin = new int[ch+1]; //creating an array for 0 to change                                store the minimum number of coins required for each term at index

        int b=ch+1;
        for(int i=0;i<b;i++)
            {
            int count = i; //This initializes the min coins to that number so it is never greater than that number itself. (but I have a doubt here: what if we don't have 1 in coins available 

            for(int j=0; j<noc; j++)
                {
                int c = ca[j]; //this takes the value of coins available from starting everytime i changes

                if((c < i) && (fin[i-c] +1 < count)) // as we using dynamic programming it starts from base case, so the best value for each number i is stored in fin[] , when we check for number i+1, it checks best case for the previous numbers.
                    count = fin[i-c]+1 ;

            }
            fin[i]= count;
        }


        for(int i=0;i<b;i++)
            {
            System.out.println(fin[i]);
        }

    }
}

I've taken reference from this page : http://interactivepython.org/runestone/static/pythonds/Recursion/DynamicProgramming.html

Can anyone help?


回答1:


The article you cited, explain well how to build a coin changer algorithm using dynamic programming. The python version of the algorithm can be translated in Java:

import java.util.Arrays;
import java.util.List;

public class CoinChanger {

    public int[] dpMakeChange(List<Integer> coinValueList, int change, int[] minCoins) {
        for (int cents = 0; cents <= change; cents++) {
            int coinCount = cents;
            for (Integer c : coinValueList) {
                if (c > cents) {
                    continue;
                }
                if (minCoins[cents - c] + 1 < coinCount) {
                    coinCount = minCoins[cents - c] + 1;
                }
            }
            minCoins[cents] = coinCount;
        }
        return minCoins;
    }

    public static void main(String[] args) {
        List<Integer> coinValueList = Arrays.asList(new Integer[]{1, 2, 3});
        int change = 10;
        int[] minCoins = new int[change + 1];
        int[] result = (new CoinChanger()).dpMakeChange(coinValueList, change, minCoins);
        for (int i = 0; i < result.length; i++) {
            System.out.println("For change = " + i + " number of coins = " + result[i]);
        }
    }
}

Using coins of value 1, 2 and 3, as in your question, the previous algorithm gives you the right values:

For change = 0 number of coins = 0
For change = 1 number of coins = 1
For change = 2 number of coins = 1
For change = 3 number of coins = 1
For change = 4 number of coins = 2
For change = 5 number of coins = 2
For change = 6 number of coins = 2
For change = 7 number of coins = 3
For change = 8 number of coins = 3
For change = 9 number of coins = 3
For change = 10 number of coins = 4


来源:https://stackoverflow.com/questions/32681464/coin-change-using-dynamic-programming

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!