繁体   English   中英

给定索引约束,找到多个数组的最大和的最佳方法

[英]Best way to find the maximum sum of multiple arrays given constraints on index

假设我有3个排序数组,每个数组的长度为4,我想从每个数组中选择一个索引,使索引的总和等于4。如何在不测试所有可能选择的情况下找到最大可能的总和?

例如我有以下数组

1 : [0,0,0,8]
2 : [1,4,5,6]
3 : [1,5,5,5]

Then the solution would be 3,0,1. Because 3 + 0 + 1 = 4 and 8 + 1 + 5 is 
the maximum combination where the sum of the indexes are 4.

我需要一个可以推广到大小为m的n个数组的解决方案,其中索引的总和可以等于任何值。

例如,可能会要求使用全部大小为1000的1000个数组解决此问题,其中索引的总和为2000。

如果某处有python软件包可以执行此操作,请告诉我。

这将实现它,不确定速度是否满足您的要求

df1=pd.DataFrame([[0,0,0,8],[1,4,5,6],[1,5,5,5]])
import functools    
df=pd.DataFrame(list(itertools.product([0,1,2,3],[0,1,2,3],[0,1,2,3])))
df=df.loc[df.sum(1)<=4,:]
df.index=df.apply(tuple,1)
df.apply(lambda x : df1.lookup(df.columns.tolist(),list(x.name)),1).sum(1).idxmax()


Out[751]: (3, 0, 1)

df.apply(lambda x : df1.lookup(df.columns.tolist(),list(x.name)),1).sum(1).max()
Out[752]: 14

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM