我想使用动态编程来查找求和到目标的数组的所有子集,例如:
arr = [1,3,2,4]
目标= 6
输出:
[1,3,2],[2,4]
如何修改代码以获取解决方案?谢谢
def CheckSubsetSum(S, M):
n = len(S)
subset = np.array([[True]*(M+1)]*(n+1))
for i in range(0,n):
subset[i,0] = True
for j in range(1,M+1):
subset[0,j] =False
for i in range(1,n+1):
for j in range(1,M+1):
if j<S[i-1]:
subset[i,j] = subset[i-1,j]
else:
subset[i,j] = subset[i-1,j] or subset[i-1,j-S[i-1]]
print(subset)
CheckSubsetSum([1,3,2,4],6)