Calculate the largest combination of names for a number
A task:
I need to get the best denomination combination for a given number. Example: I have three names {50,25,10}
, and the given number is 30, then the list should return <10,10,10>
. for number 80 it should return <50,10,10,10>
as the remaining 30 is not fully divisible by 25. For 35 it should return <25,10>
for 75 <50,25>
for 65<50,10>
This is somewhat similar to the coin issue, but I cannot get the denomination values.
Refence StackOverFlow Coin DP Replacement Solution for Coin Tracking Here is what I have tried so far:
public static int[] minChange(int[] denom, int changeAmount) {
int n = denom.length;
int[] count = new int[changeAmount + 1];
int[] from = new int[changeAmount + 1];
count[0] = 1;
for (int i = 0; i < changeAmount; i++ )
if (count[i] > 0)
for (int j = 0; j < n; j++ ) {
int p = i + denom[j];
if (p <= changeAmount) {
if (count[p] == 0 || count[p] > count[i] + 1) {
count[p] = count[i] + 1;
from[p] = j;
}
}
}
// No solutions:
if (count[changeAmount] == 0)
return null;
// Build answer.
int[] result = new int[count[changeAmount] - 1];
int k = changeAmount;
while (k > 0) {
result[count[k] - 2] = denom[from[k]];
k = k - denom[from[k]];
}
return result;
}
This works well if the value is completely divisible by one of the merits of the other, and I don't work at all.
source to share