Complete the code to initialize the DP array for the unbounded knapsack problem.
dp = [0] * ([1] + 1)
The DP array size depends on the maximum weight capacity W, so we create an array of size W + 1.
Complete the code to iterate over all weights from 1 to W.
for weight in range(1, [1] + 1):
We iterate from 1 to the maximum capacity W to fill the DP array.
Fix the error in the condition to check if the current item can fit in the knapsack.
if weights[i] [1] weight:
The item can be included if its weight is less than or equal to the current weight capacity.
Fill both blanks to update the DP array with the maximum value including the current item.
dp[weight] = max(dp[weight], dp[weight [1] weights[i]] [2] values[i])
We subtract the item's weight from the current weight to find the remaining capacity, then add the item's value to the DP value at that remaining capacity.
Fill all three blanks to complete the unbounded knapsack function.
def unbounded_knapsack([1], [2], [3]): dp = [0] * (W + 1) for weight in range(1, W + 1): for i in range(len(weights)): if weights[i] <= weight: dp[weight] = max(dp[weight], dp[weight - weights[i]] + values[i]) return dp[W]
The function takes three parameters: weights list, values list, and the maximum capacity W.
