0

My program is tasked to count all possible combinations of getting the value of T, with a list of coins with varying (and some identical) values with limited amount of coins.

Two coins with different names could have a same value, for example both "Gold" and "Topaz" coins both have a value of 3.

public static int change(int target, ArrayList<GemCoin> coinStocks) {
    int[] combi = new int[target + 1];
    combi[0] = 1;
    for (GemCoin g: coinStocks) {
        for (int i = 1; i < combi.length; i++) {
            if (i >= g.getValue() && g.getStocks() > 0) {  ///if
                combi[i] += combi[i - d.getChips()];
            }
        }
    }
    for(int im : combi){
        System.out.println(im);
    }
    return combi[target];
}

That's my general idea, but i do notice that the coin stocks "ran out" before reaching the last index in combi, even though there exists a combination involving that coin.

Lil Student
  • 17
  • 1
  • 4
  • You say your function should return a list of combinations, but it returns a single integer. Please could you provide some sample input and the desired output – Simon Crane Sep 27 '19 at 09:48
  • 1
    i'm sorry, "how many" combinations – Lil Student Sep 27 '19 at 13:39
  • 1
    for example, Topaz (T) Coin is $1, Ruby (R) Coin is $1, Sapphire(S) Coin is $2, Gold(G) Coin is $3. There are 4 Topaz, 1 Ruby, 1 Sapphire and 1 Gold. The target is $4. To get that you can use 4T/3T1R/2T1S/1G meaning 4 is returned as output – Lil Student Sep 27 '19 at 13:49
  • Could you post the code for your GemCoin class? – Simon Crane Sep 27 '19 at 14:01
  • There are 6 combinations: 4T, 3T1R, 2T1S, 1T1R1S, 1T1G, 1R1G. – maraca Sep 28 '19 at 11:02
  • Where does d.getChips() come from? If the quantity is a property of the GemCoin class then your algorithm doesn't work as intended. If not then it can work with a minor modification, but it wouldn't give you the desired result, e.g. 4T and a target of 3 would give you 4 as answer and not 1, because you treat each coin as unique, even if they are all T. – maraca Sep 28 '19 at 11:20

0 Answers0