Knapsack algorithm

mail@pastecode.io avatar
unknown
c_cpp
3 years ago
510 B
9
Indexable
Never
int knapsack(int totalWeight, int weight[], int value[], int item) {
    int k[item+1][totalWeight+1];

    for(int i = 0; i <= item; i++) {
        for(int j = 0; j <= totalWeight; j++) {
            if(i == 0 || j == 0) {
                k[i][j] = 0;
            } else if(weight[i-1] <= j) {
                k[i][j] = max(value[i-1] + k[i-1][j-weight[i-1]], k[i-1][j]);
            } else {
                k[i][j] = k[i-1][j];
            }
        }
    }
    return k[item][totalWeight];
}