Answers for "0-1 knapsack problem dynamic programming using single array"

0

0-1 knapsack problem dynamic programming using single array

int dp[MAXW];
int solve()
{
    memset(dp, 0, sizeof(dp));
    for(int i =1; i <= N; i++) {
        for(int j = W; j >= 0; j--) {
            dp[j] = (w[i] > j) ? dp[j]: max(dp[j], dp[j-w[i]] + v[i]);
        }
    }
    return dp[W];
}
Posted by: Guest on March-12-2021

Code answers related to "0-1 knapsack problem dynamic programming using single array"

Browse Popular Code Answers by Language