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
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 :-
- knapsack capacity :- Amount needed for example 110
- items available:- x1*10,x2*20....xn*100. Cost and weight of items as same.
- Solve Knapsack for max profit using DP solution
- If max profit == knapsack capacity then you have a solution so retrace it using DP matrix.
- 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