Posts Unbounded Knapsack
Post
Cancel

Unbounded Knapsack

Problem Description

Given weights and values of n items, put these items in a knapsack of capacity W to get the maximum total value in the knapsack. In other words, given two integer arrays val[0..n-1] and wt[0..n-1] which represent values and weights associated with n items respectively. Also given an integer W which represents knapsack capacity, find out the maximum value subset of val[] such that sum of the weights of this subset is smaller than or equal to W. You cannot break an item, either pick the complete item or don’t pick it (0-1 property).

geeksforgeeks

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class Solution{
    
    static int knapSack(int N, int W, int val[], int wt[])
    {
        
        int[][] dp = new int[N+1][W+1];
        
        for(int i=0; i<=N; i++){
            Arrays.fill(dp[i], -1);
        }
        
        KP(N, W, val, wt, dp);
        
        return dp[N][W];
        
    }
    
    static int KP(int i, int j, int[] v, int[] w, int[][] dp){
        
        if(i == 0){
            return 0;
        }
        
        if(dp[i][j] == -1){
            
            //leave
            int x = KP(i-1, j, v, w, dp);
            
            //pick
            //Since we can choose the same element again, when picking it up, we don't decrease the ith index.
            if(j>= w[i-1]){
                x = Math.max(x, KP(i, j-w[i-1], v, w, dp) + v[i-1]);
            }
            
            dp[i][j] = x;
            
        }
        
        return dp[i][j];
        
    }
    
}
This post is licensed under CC BY 4.0 by the author.