
Let ‘WEIGHT’ = [1, 2, 3], ‘VALUE’ = [10, 20, 30] and ‘W’ = 4.
In this case, Ninja can take item 1 and item 3. Then the sum of weights will be 1 + 3 = 4, and the sum of values will be 10 + 30 = 40, which is the maximum possible sum of values Ninja can take home.
The first line contains an integer 'T', which denotes the number of test cases.
The next line contains two integers ‘N’ and ‘W’ denoting the number of items and the maximum total weight of items Ninja can take home respectively.
The next line contains ‘N’ space-separated positive integers denoting the elements of array ‘WEIGHT’.
The next line contains ‘N’ space-separated positive integers denoting the elements of array ‘VALUE’.
For each test case, return the maximum possible sum of values of items that Ninja takes home.
You don't need to print anything, it has already been taken care of. Just implement the given function.
1 ≤ T ≤ 10
1 ≤ N ≤ 50
1 ≤ W ≤ 10^7
1 ≤ WEIGHT[i] ≤ W
1 ≤ VALUE[i] ≤ 100
WEIGHT.length = VALUE.length = N
Time limit: 1 sec
A simple solution is to generate weights for all possible values of items and take the one which has sum of values as maximum and total weight less than or equal to ‘W’.
The maximum total value of items will be simply sum of values of all items. We can loop through all these values and check for a particular value what weight these items have. This weight must be less than or equal to our knapsack capacity ‘W’. And we take a maximum of all these values.
Now, for finding the total weight for a particular value we can use recursion because we will generate all possible subsets of items. We are just finding what is the least weight Ninja need to carry to get a particular value.
For i-th item there will be two choices:
Then we will just take the minimum of these two cases as we want to find the least weight Ninja need to carry.
The solution which we discussed in the previous approach was exponential and will result in a time-out. If we loop closely there will be many overlapping subproblems in that problem. Hence if we store result calculated for a particular state we might use that later.
We can memoize the above solution. As there are two variables changing there will be two states in our DP table.
The steps are as follows :
In this approach, we will construct our DP table in a bottom-up manner. We will calculate all minimum weights for all values of items from 0 to the sum of all values of items.
Initially, it will be filled with infinity because it will help in comparing minimum weights.
Base condition will be DP[0][j] = 0 for all j in [0, .. maximumValue], because we can get 0 minimum weight by taking 0 items.
DP[i][j] can be calculated by either including ‘i’-th item or excluding ‘i’-th item.
So, DP[i][j] will be initially equal to DP[i - 1][j] (Excluding)
if VALUE[i] <= j, which means we can include ‘i’-th item, then we will take minimum of (DP[i][j], DP[i - 1][j - VALUE[i]] + WEIGHT[i]).
After generating all minimum weights we will check for which values the weight is less than or equal to ‘W’. And all of these values we will take the maximum value.
The steps are as follows :