public static int backtrackingCalculation(List items, int capacity) {

int with = 0;
int without = 0;
int capa = capacity;

while (items.size() > 0) {

if (items.get(0).getWeight() < capacity) {
with += items.get(0).getValue();
capacity -= items.get(0).getWeight();
items.remove(0);
with += backtrackingCalculation(items, capacity);
without += backtrackingCalculation(items, capa);
} else {
items.remove(0);
with+= backtrackingCalculation(items, capacity);
without += backtrackingCalculation(items, capa);
}

}

if (without > with) {
return without;
}
return with;

}

Habe eine neue Variante. Ich habe versucht jede mögliche Kombination durchrechnen zu lassen und die Beste dann auszusuchen.

with bedeutet ich nehme das item mit rein und without bedeutet ich nehme es nicht mit rein.

Aber auch das funktioniert nicht. Es berechnet zwar einiges aber das richtige Ergebnis wird auch nicht gefunden.

...zur Antwort