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
0
akkeshri14042001in 8 hours
int knapSack(int N, int W, int val[], int wt[])
   {
       // code here
       vector<vector<int>>dp(N+1,vector<int>(W+1));
       for(int i=0;i<N+1;i++){
           for(int j=0;j<W+1;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(j>=wt[i-1]){
                   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
sarthakpant31in 59 minutes
class Solution{
public:
    int knapSack(int N, int W, int val[], int wt[])
    {
        // code here
        
        vector<vector<int>> dp(N+1,vector<int>(W+1,0));
        
        
        for(int i=1;i<=N;++i){
            for(int j=1;j<=W;++j){
                if(wt[i-1]>j){
                    dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
                }
                else{
                    dp[i][j]=max(dp[i-1][j],dp[i][j-wt[i-1]]+val[i-1]);
                }
            }
        }
        
        return dp[N][W];
    }
};
0
tanvib20141 day ago
class Solution{
public:
    int knapSack(int N, int W, int val[], int wt[])
    {
        vector<int> dp(W+1, 0);
        for(int j=0; j<N; j++){
            for(int i=1; i<=W; i++){
                if(wt[j]<=i)dp[i]=max(dp[i],dp[i-wt[j]]+val[j]);
            }
        }
        return dp[W];
    }
};
0
anurondas6762 days ago
Clean and simple dp solution:
int knapSack(int N, int W, int val[], int wt[])   {       // code here       vector<int> dp(W+1,INT_MIN);       dp[0] = 0;       for(int i=1;i<=W;++i){           for(int j=0;j<N;++j){               if(i-wt[j] >= 0){                   dp[i] = max(dp[i],val[j]+dp[i-wt[j]]);               }           }           if(dp[i]==INT_MIN) dp[i]=0;       }       return dp[W];   }
0
chiraggoel162 days ago
Can someone add DP to this code please!?
int helper(int weightLeft, int profit, int N, int val[], int wt[]){
       if(weightLeft == 0)
           return profit;
       if(weightLeft < 0)
           return 0;
       int ans = INT_MIN;
       for(int i=0;i<N;i++){
           int temp = helper(weightLeft-wt[i],profit+val[i],N,val,wt);
           ans = max(ans, temp);
       }
       return ans;
   }

   int knapSack(int N, int W, int val[], int wt[])
   {
       // code here
       d
       return helper(W,0,N,val,wt);
   }
0
2019sushilkumarkori5 days ago
int knapSack(int n, int W, int val[], int wt[])
    { 
       // Your code here
       int t[n+1][W+1];
       for(int i=0;i<=n;i++){
           t[i][0] = 0;
       }
       for(int i=1;i<=W;i++){
           t[0][i] = 0;
       }
       for(int i=1;i<=n;i++){
           for(int j=1;j<=W;j++){
               if(wt[i-1]<=j){
                   t[i][j] = max(t[i-1][j],t[i][j-wt[i-1]]+val[i-1]);
               }
               else{
                   t[i][j] = t[i-1][j];
               }
           }
       }
       return t[n][W];
    }
0
tahabasra925 days ago
Top down recursive DP solution in c++
 
 
int solve(int val[],int wt[],int i,int W,int curr_w,int n,vector<vector<int>> &dp){       if(curr_w==W){           return 0;       }         if(i==n){           if(curr_w<W){               return 0;           }           return INT_MIN;       }       if(curr_w>W){           return INT_MIN;       }              if(dp[i][curr_w]!=-1){           return dp[i][curr_w];       }       int a=val[i]+solve(val,wt,i,W,curr_w+wt[i],n,dp);       int b=solve(val,wt,i+1,W,curr_w,n,dp);       return dp[i][curr_w]=max(a,b);   }   int knapSack(int N, int W, int val[], int wt[])   {       // code here        vector<vector<int>> dp(N+1, vector<int>(W+1, -1));       int ans= solve(val,wt,0,W,0,N,dp);       if(ans<0){           return 0;       }       return ans;   }
 
0
tkuchchal5305 days ago
##PYTHON
class Solution:   def knapSack(self, N, W, val, wt):       # code here       t=[[0 for i in range(W+1)] for j in range(N+1)]       for i in range(1,N+1):           for j in range(1,W+1):               if wt[i-1]<=j:                   t[i][j]=max(val[i-1]+t[i][j-wt[i-1]],t[i-1][j])               else:                   t[i][j]=t[i-1][j]       return t[N][W]
Test Cases Passed:
205 / 205
Total Time Taken:
0.5/2.58
 
+1
manjunatha_kb1 week ago
Java
class Solution{
    static int knapSack(int N, int W, int val[], int wt[])
    {
        // code here
        int[][] dp = new int[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] = Math.max(dp[i][j - wt[i-1]] + val[i-1], dp[i-1][j]);
                } else {
                    dp[i][j] = dp[i-1][j];
                }
                
            }
        }
        
        return dp[N][W];
    }
}
-1
parasdhanwal2 weeks ago
Java Solution | Memoization
 
class Solution{
    static int knapSack(int N, int W, int val[], int wt[])
    {
        int dp[][]=new int [N][W+1];
        for(int rows[]:dp)
        Arrays.fill(rows,-1);
        return knapSack(N-1,W,val,wt,dp);
    }
    
    static int knapSack(int ind, int W, int val[], int wt[],int [][]dp)
    {
        if(ind==0)
        {
            int ans=(int)((W/wt[ind])*val[ind]);
            return ans;
        }
        
        if(dp[ind][W]!=-1) return dp[ind][W];
        
        int nt=knapSack(ind-1,W,val,wt,dp);
        int t=0;
        if(W-wt[ind]>=0)
        t=val[ind]+knapSack(ind,W-wt[ind],val,wt,dp);
        
        return dp[ind][W]=Math.max(nt,t);
    }
}
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.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems 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.