Can someone please help me understand what is wrong with this solution for 0/1 Knapsack problem?
static int knapsackRecursive(final int[] v, final int[] w, final int c, final int beg) {
if (c == 0 || beg >= v.length) {
return 0;
}
int max = Integer.MIN_VALUE;
for (int i = beg; i < v.length; ++i) {
if (w[i] <= c) {
int ans = v[i] + knapsackRecursive(v, w, c - w[i], (i + 1));
max = Math.max(max, ans);
}
}
return max;
}
static int knapsackRecursiveCaller(final int[] v, final int[] w, final int c) {
return knapsackRecursive(v, w, c, 0);
}
I've seen this link, https://www.***.org/0-1-knapsack-problem-dp-10/, and I think my approach accomplishes the same thing. Can someone help me understand what is wrong with my code?