Dynamic Programming: Knapsack - Equal Partition (Partition Equal Subset Sum)
The following code attempts to solve the Equal Partition problem using a 1D DP array. Which line contains a subtle bug that can cause incorrect results on some inputs?
def canPartition(nums):
total = sum(nums)
target = total // 2
dp = [False] * (target + 1)
dp[0] = True
for num in nums:
for w in range(num, target + 1):
dp[w] = dp[w] or dp[w - num]
return dp[target]
