Knapsack with Duplicate Items | Practice | GeeksforGeeks

Given a set of N items, each with a weight and a value, represented by the array w[] and val[] respectively. Also, a knapsack with weight limit W.
The task is to fill the knapsack in such a way that we can get the maximum profit. Return the maximum profit.
Note: Each item can be taken any number of times.
 
Example 1:
Input: N = 2, W = 3
val[] = {1, 1}
wt[] = {2, 1}
Output: 3
Explanation: 
1.Pick the 2nd element thrice.
2.Total profit = 1 + 1 + 1 = 3. Also the total 
  weight = 1 + 1 + 1  = 3 which is <= W.

 
Example 2:
Input: N = 4, W = 8
val[] = {1, 4, 5, 7}
wt[] = {1, 3, 4, 5}
Output: 11
Explanation: The optimal choice is to 
pick the 2nd and 4th element.
 
Your Task:
You do not need to read input or print anything. Your task is to complete the function knapSack() which takes the values N, W and the arrays val[] and wt[] as input parameters and returns the maximum possible value.
 
Expected Time Complexity: O(N*W)
Expected Auxiliary Space: O(W)
 
Constraints:
1 ≤ N, W ≤ 1000
1 ≤ val[i], wt[i] ≤ 100
+1
adarshgupta4013 days ago
DP Java Code-
class Solution{
    static int knapSack(int N, int W, int val[], int wt[])
    {
        //multiple occurences of an item allowed in sum
        int [][] dp = new int[N+1][W+1];
        for(int i = 0 ; i<W+1 ; i++) dp[0][i] = 0;
        for(int i = 0 ; i<N+1 ; i++) dp[i][0] = 0;
        
        for(int i=1 ; i<N+1 ; i++){
            for(int j= 1; j<W+1 ; j++){
                if(wt[i-1] <= j ){
                    dp[i][j] = Math.max(val[i-1]+dp[i][j-wt[i-1]] , // if it is taken in bag then can be taken again 
                                dp[i-1][j] ); //if it is not taken then its processed , do not need to take it again.
                } else{
                    dp[i][j] = dp[i-1][j]; 
                }
            }
        }
        return dp[N][W];
        
    }
}
0
arghya2k011 week ago
int knapSack(int n, int w, int val[], int wt[])
    {
        vector<int>prev(w+1,0);
	for(int i=0;i<=w;i++)prev[i]=(i/wt[0])*val[0];
	
	for(int i=1;i<n;i++){
		for(int j=1;j<=w;j++){
			int notpick=prev[j];
			int pick=0;
			if(j>=wt[i])pick=val[i]+prev[j-wt[i]];
			prev[j]=max(pick,notpick);
		}
	}
	return prev[w];
    }
0
annanyamathur1 week ago
int memo[1002][1002];
    int knap(int n, int w, int val[], int wt[])
    {
         if(memo[n][w]==-1)
    {
        if(n==0 ||w==0)
        memo[n][w]= 0;
      else  if(wt[n-1]<=w)
        {
            memo[n][w]= max(knap(n-1,w,val,wt),max(val[n-1]+knap(n,w-wt[n-1],val,wt),val[n-1]+
                       knap(n-1,w-wt[n-1],val,wt)));
        }
        else
        {
          memo[n][w]=  knap(n-1,w,val,wt);
        }
        
    }
        return memo[n][w];
    }
    int knapSack(int n, int w, int val[], int wt[])
    {
        memset(memo,-1,sizeof(memo));
       return knap(n,w,val,wt);
    
    }
+1
jainmuskan5652 weeks ago
int dp[1002][1002];   int solve(int N, int W, int val[], int wt[]){       // initialize       for(int i=0;i<=N;i++){           for(int j=0;j<=W;j++){               if(i==0||j==0){                   dp[i][j]=0;               }           }       }       for(int i=1;i<=N;i++){           for(int j=0;j<=W;j++){                              if(wt[i-1]<=j){                   dp[i][j]=max(val[i-1]+dp[i][j-wt[i-1]],dp[i-1][j]);               }               else{                   dp[i][j]=dp[i-1][j];               }           }       }       return dp[N][W];   }   int knapSack(int N, int W, int val[], int wt[])   {       memset(dp,-1,sizeof(dp));       return solve(N,W,val,wt);   }
0
devashishbakare2 weeks ago
Java DP Tabulation
 
static int knapSack(int n, int cap, int val[], int wt[])
    {
        if(n == 0)
        return 0;
        //we have to solve smaller problem first
        //so there ans should be stored one place hence this array created(Tabulation)
        int dp[] = new int[cap+1];
        
        dp[0] = 0;
        
        //travers through capsity
        for(int i = 1; i < cap+1; i++)
        {
            //max for getting maximum outcome from all weights
            int max = 0;
            //taking one weight at a time and geting the profit
            for(int j = 0; j < wt.length; j++)
            {
                //cap should be greater than the weight
                if(i >= wt[j])
                {
                    //previosly possible max weight
                    int prevMax = dp[i-wt[j]];
                    //adding current weight
                    int currMax = val[j] + prevMax;
                    //if maximum then update
                    if(currMax > max)
                    max = currMax;
                    
                    //as we are exploring all the weight hence declaring max above the for loop
                }
                
                
            }
            dp[i] = max; 
        }//return final capacity
        return dp[cap];
    }
0
onlyprofessionalmails1 month ago
int knapSack(int N, int W, int val[], int wt[])   {       int dp[N + 1][W + 1];       for(int i = 0 ;i < W + 1; i++){           dp[0][i] = 0;       }       for(int j = 0; j < N + 1; j++){           dp[j][0] = 0;       }       for(int i = 1; i < N + 1; i++){           for(int j = 1; j < W + 1; j++){               if(wt[i - 1] <= j){                   dp[i][j] = max(val[i - 1] + dp[i][j - wt[i - 1]], dp[i - 1][j]);               }else{                   dp[i][j] = dp[i - 1][j];               }           }       }       return dp[N][W];   }
0
ayushkumar54511 month ago
 int knapSack(int N, int W, int val[], int wt[])   {       for(int i=1;i<=N;i++){           for(int j=0;j<=W;j++){               if(i==0 or j==0) dp[i][j]=0;                          }       }       for(int i=1;i<N+1;i++){           for(int j=1;j<W+1;j++){               if(wt[i-1]<=j){                   dp[i][j]=max(val[i-1]+dp[i][j-wt[i-1]],dp[i-1][j]);               }               else{                   dp[i][j]=dp[i-1][j];               }           }       }       return dp[N][W];   }
+4
debajyoti dev1 month ago
Question is common. But DP cant be consider as EASY ! no beginner can undestand DP funda.  Thats why LeetCode level is reliable. not GFG.
0
sangamchoudhary72 months ago
class Solution{
public:
    int dp[1001][1001];
    int solve(int n,int w,int val[],int wt[]){
        if(w >= 0 and n < 0) return 0;
        else if(w < 0) return INT_MIN;
        else if(dp[n][w] != -1) return dp[n][w];
        
        int op1 = val[n] + solve(n-1,w-wt[n],val,wt);
        int op2 = val[n] + solve(n,w-wt[n],val,wt);
        int op3 = solve(n-1,w,val,wt);
        
        int ans = max({op1,op2,op3});
        
        return dp[n][w] = ans;
    }

    int knapSack(int N, int W, int val[], int wt[]){
        memset(dp,-1,sizeof dp);
        return solve(N-1,W,val,wt);
    }
};
+2
mithileshkumarsingh2 months ago
int solve(int wt[], int val[], int w, int n)
{
    //using top down approach
    int dp[n + 1][w + 1];
    for (int i = 0; i <= n; i++)
    {
        for (int j = 0; j <= w; j++)
        {
            if (i == 0 || j == 0)
                dp[i][j] = 0;
            else if (wt[i - 1] <= j)
                dp[i][j] = max(val[i - 1] + dp[i][j - wt[i - 1]], dp[i - 1][j]);
            else
                dp[i][j] = dp[i - 1][j];
        }
    }
    return dp[n][w];
}
    int knapSack(int N, int W, int val[], int wt[]){
        int ans=solve(wt,val,W,N);
           return ans;
    }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.