I am not getting right answer with this approach, May I know the reason?
public static int solveKnapSacksDPTB(int[] profits, int[] weights, int C) {
int index = 0, currentCapacity = 0, currentProfit = 0;
Integer[][] dp = new Integer[weights.length + 1][C + 1];
return solveKnapSacksDPTB(profits, weights, index, currentCapacity, currentProfit, C, dp);
}
private static int solveKnapSacksDPTB(int[] profits, int[] weights, int index, int currentCapacity,
int currentProfit, int C, Integer[][] dp) {
if (index >= weights.length) {
return currentProfit;
}
if (currentCapacity > C) {
return 0;
}
if (dp[index][currentCapacity] != null) {
return dp[index][currentCapacity];
}
int profit1 = solveKnapSacksDPTB(profits, weights, index, currentCapacity + weights[index],
currentProfit + profits[index], C, dp);
int profit2 = solveKnapSacksDPTB(profits, weights, index + 1, currentCapacity, currentProfit, C, dp);
dp[index][currentCapacity] = max(profit1, profit2);
return dp[index][currentCapacity];
}