Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/dynamicprogramming/KnapsackMemoization.java (Java)

Code before:
        }

        return solveKnapsackRecursive(capacity, weights, profits, numOfItems, dpTable);
    }

    // Returns the value of maximum profit using recursive approach
    int solveKnapsackRecursive(int capacity, int[] weights, int[] profits, int numOfItems, int[][] dpTable) {
        // Base condition
        if (numOfItems == 0 || capacity == 0) {
            return 0;
        }

        if (dpTable[numOfItems][capacity] != -1) {
            return dpTable[numOfItems][capacity];
        }

        if (weights[numOfItems - 1] > capacity) {
            // Store the value of function call stack in table
            dpTable[numOfItems][capacity] = solveKnapsackRecursive(capacity, weights, profits, numOfItems - 1, dpTable);
            return dpTable[numOfItems][capacity];
        } else {
            // case 1. include the item, if it is less than the capacity
            int includeCurrentItem = profits[numOfItems - 1] + solveKnapsackRecursive(capacity - weights[numOfItems - 1], weights, profits, numOfItems - 1, dpTable);

            // case 2. exclude the item if it is more than the capacity
            int excludeCurrentItem = solveKnapsackRecursive(capacity, weights, profits, numOfItems - 1, dpTable);

            // Store the value of function call stack in table and return
            dpTable[numOfItems][capacity] = Math.max(includeCurrentItem, excludeCurrentItem);
            return dpTable[numOfItems][capacity];
        }
    }
}


Review comment (suggestion):
```suggestion
            final int excludeCurrentItem = solveKnapsackRecursive(capacity, weights, profits, numOfItems - 1, dpTable);
```

Code after:
        }

        return solveKnapsackRecursive(capacity, weights, profits, numOfItems, dpTable);
    }

    // Returns the value of maximum profit using recursive approach
    int solveKnapsackRecursive(int capacity, int[] weights, int[] profits, int numOfItems, int[][] dpTable) {
        // Base condition
        if (numOfItems == 0 || capacity == 0) {
            return 0;
        }

        if (dpTable[numOfItems][capacity] != -1) {
            return dpTable[numOfItems][capacity];
        }

        if (weights[numOfItems - 1] > capacity) {
            // Store the value of function call stack in table
            dpTable[numOfItems][capacity] = solveKnapsackRecursive(capacity, weights, profits, numOfItems - 1, dpTable);
            return dpTable[numOfItems][capacity];
        } else {
            // case 1. include the item, if it is less than the capacity
            final int includeCurrentItem = profits[numOfItems - 1] + solveKnapsackRecursive(capacity - weights[numOfItems - 1], weights, profits, numOfItems - 1, dpTable);

            // case 2. exclude the item if it is more than the capacity
            final int excludeCurrentItem = solveKnapsackRecursive(capacity, weights, profits, numOfItems - 1, dpTable);

            // Store the value of function call stack in table and return
            dpTable[numOfItems][capacity] = Math.max(includeCurrentItem, excludeCurrentItem);
            return dpTable[numOfItems][capacity];
        }
    }
}