ATM algorithm of giving money with limited amount of bank notes

后端 未结 2 597
无人及你
无人及你 2021-02-06 10:54

All the change-making problem in the web talk only about ideal situation where we have unlimited ammount of coins/banknotes of every kind.

I want to deal with situation

相关标签:
2条回答
  • 2021-02-06 11:35

    It can be done relatively easily, you just have to keep trying adding bank notes that left in every possibility and then discard possibilities, which already have more than you try to achieve.

    This is working code, values are "bank notes" values and in "ammounts" are ammounts of bank notes you have :

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    public class JavaApplication55 {
        int[] values = {10,20,50,100,200};
    
        public static void main(String[] args) {
            int[] values = {10,20,50,100,200};
            int[] ammounts = {10,10,10,10,10};
            List<Integer[]> results = solutions(values, ammounts, new int[5], 180, 0);
            for (Integer[] result : results){
                System.out.println(Arrays.toString(result));
            }
    
        }
    
        public static List<Integer[]> solutions(int[] values, int[] ammounts, int[] variation, int price, int position){
            List<Integer[]> list = new ArrayList<>();
            int value = compute(values, variation);
            if (value < price){
                for (int i = position; i < values.length; i++) {
                    if (ammounts[i] > variation[i]){
                        int[] newvariation = variation.clone();
                        newvariation[i]++;
                        List<Integer[]> newList = solutions(values, ammounts, newvariation, price, i);
                        if (newList != null){
                            list.addAll(newList);
                        }
                    }
                }
            } else if (value == price) {
                list.add(myCopy(variation));
            }
            return list;
        }    
    
        public static int compute(int[] values, int[] variation){
            int ret = 0;
            for (int i = 0; i < variation.length; i++) {
                ret += values[i] * variation[i];
            }
            return ret;
        }    
    
        public static Integer[] myCopy(int[] ar){
            Integer[] ret = new Integer[ar.length];
            for (int i = 0; i < ar.length; i++) {
                ret[i] = ar[i];
            }
            return ret;
        }
    }
    

    This code having this ouput (it is output for 10,20,50,100,200 bank notes, you have 10 of each of them and you want to get 180 in sum)

    [10, 4, 0, 0, 0]
    [9, 2, 1, 0, 0]
    [8, 5, 0, 0, 0]
    [8, 0, 2, 0, 0]
    [8, 0, 0, 1, 0]
    [7, 3, 1, 0, 0]
    [6, 6, 0, 0, 0]
    [6, 1, 2, 0, 0]
    [6, 1, 0, 1, 0]
    [5, 4, 1, 0, 0]
    [4, 7, 0, 0, 0]
    [4, 2, 2, 0, 0]
    [4, 2, 0, 1, 0]
    [3, 5, 1, 0, 0]
    [3, 0, 3, 0, 0]
    [3, 0, 1, 1, 0]
    [2, 8, 0, 0, 0]
    [2, 3, 2, 0, 0]
    [2, 3, 0, 1, 0]
    [1, 6, 1, 0, 0]
    [1, 1, 3, 0, 0]
    [1, 1, 1, 1, 0]
    [0, 9, 0, 0, 0]
    [0, 4, 2, 0, 0]
    [0, 4, 0, 1, 0]
    
    0 讨论(0)
  • 2021-02-06 11:52

    Making amount from given set of coins is n-p complete problem because it reduces to subset sum problem or knapsack problem. But you have a pseudo polynomial time algorithm for the knapsack problem which is efficient enough for your purpose. Here you are using a greedy algorithm which gives solutions which can give solution for most cases but fails for some and hence cannot be used but you can use a combination of the pseudo polynomial time algorithm and the greedy to get an efficient algorithm that solves for all cases with high speed solution for best cases.

    Pseudo polynomial time solution using knapsack analogy :-

    1. knapsack capacity :- Amount needed for example 110
    2. items available:- x1*10,x2*20....xn*100. Cost and weight of items as same.
    3. Solve Knapsack for max profit using DP solution
    4. If max profit == knapsack capacity then you have a solution so retrace it using DP matrix.
    5. else there is no way you can get the amount using current available coins.

    Time complexity:- O(Amount*Items)

    Combination of greedy and DP solution :-

    boolean greedysolve = false, DPsolve = false;
    
    greedysolve = YourSolution();
    
    if(!greedysolve) {
    
       DPsolve = KnapsackSolution(); 
    }
    
    else {
    
       return(greedy_solution);
    }
    
    if(!DPsolve) {
    
       return(DPsolution);
    
    }
    
    return(null);  // No solution
    
    0 讨论(0)
提交回复
热议问题