#include <bits/stdc++.h>
int func(int index,vector<int> &weight, vector<int> value, int n, int maxWeight,int sum,vector<vector<int>> &dp,int val)
{
if(index==n)return val;
if(dp[index][sum]!=-1)return dp[index][sum];
int pick=INT_MIN,notpick=INT_MIN;
if(sum+weight[index]<=maxWeight)
pick=func(index+1,weight,value,n,maxWeight,sum+weight[index],dp,val+value[index]);
notpick=func(index+1,weight,value,n,maxWeight,sum,dp,val);
return dp[index][sum]=max(pick,notpick);
}
int knapsack(vector<int> weight, vector<int> value, int n, int maxWeight)
{
// Write your code here
// in val=INT_MIN;
vector<vector<int>>dp(n+1,vector<int>(maxWeight+1,-1));
return func(0,weight,value,n,maxWeight,0,dp,0);
}
This is the standard knapsack problem implemented above. This code passed quite a few cases but it is failing in some and I can't seem to understand the issue with this approach. Someone please help try and keep the changes as minimal as possible as i want to know what is wrong with this approach