educative.io

Mistake in the code while filling the table in a bottom-up approach

//if we have only one weight, we will take it if it is not more than the capacity

for(int c=1; c <= capacity; c++) {
if(weights[0] <= c)
dp[0][c] = profits[0];
}

This should be the code instead of this :

for(int c=0; c <= capacity; c++) {
if(weights[0] <= c)
dp[0][c] = profits[0];
}