educative.io

Educative

Why we set profit[0] for all the dp[0][c]?

Why we set profit[0] for all the dp[0][c] ?

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

for(int c=0; c <= capacity; c++) {

  if(weights[0] <= c)

    dp[0][c] = profits[0];

}

Hi Manisha!
We’re adding the value of dp[0][c] as dp[0][c] because we’re assuming this scenario for a single weight plus it is not more than the mentioned capacity. So, we can set the first row to be the initial profit which is profits[0].

Hope it will clear your confusion. If you still have any issues please let us know.
Thank you.