对于一组整数,获取所有总和大于某些值的集合
首先,让我说我找到了许多问题和答案,这些问题和答案似乎有些相关或与这个问题有些相关,但实际上并不是这个问题。它似乎与硬币变更 /子集总和问题有关,但是我认为这完全不同,子集总和答案不涵盖这个问题。
问题
是说我有一组4个名为 s 的整数:[1,2,5,1]
目标是设计一个 g 的集集由 s 的每个子集组成,其中集合的总和大于某些值 n 。
在此示例中,如果 n = 6 ,则 g 将为[[5,2,1,1],[5,2,1],[ 5,2],[5,1,1]]
注意事项
我在这里选择了一个单词,因为每组的顺序根本无关紧要。 [5,2,1,1]
与[5,1,1,2]
相同。
当您创建超过 n 的第一组时,可以说 g 1 ,您也可以立即添加所有包含 g的所有其他集合 1 作为子集。同样,如果您找到了另一个新集 g 2 尚未添加并且也超过 n ,则可以立即添加所有包含<的集合强> g 2 作为子集。您无需检查这些集合是否总和大于 n 。
如果您总结了 s 的所有成员,并且结果不大于 n ,那么您可以得出结论,没有符合标准的集合。
实际用例
在这里试图实现的现实世界目标是我有一组具有与之相关的数值的项目,而另一个代表阈值的值。我试图找出是否可以例行地找到所有可以超过阈值的子组的子群,当它们的权重求和时。
生成符合此标准的所有集合的最有效方法是什么?这是什么复杂性?
Let me start by saying I have found many questions and answers that answer things which seem somewhat related or close to this question, but are not actually this question. It seems related to the coin change / subset sum problem, but I think this is distinctly different enough that subset sum answers do not cover this question.
The Problem
Let's say I have a set of 4 integers called S : [1, 2, 5, 1]
The goal is to devise a set of sets, G , that is comprised of each subset of S where the sum of the set is greater than some value N.
In this example, if N = 6, then G would be [ [5,2,1,1], [5,2,1], [5,2], [5,1,1] ]
Considerations
I chose the word set here specifically because the order of each set does not matter at all. [5, 2, 1, 1]
is identical to [5, 1, 1, 2]
.
When you create your first set that exceeds N, lets say G1, you can also immediately add all other sets which contain G1 as a subset. Likewise, if you find another new set G2 that has not already added and also exceeds N, you can immediately add all sets which contain G2 as a subset. You don't need to check if these sets sum to greater than N.
If you sum all the members of S and the result is not greater than N, then you can conclude there are no sets which meet the criteria.
Actual Use-case
The real-world goal that is trying to be met here is that I have a group of items which have a numerical weight associated with them, and another value which represents a threshold. I'm trying find out if its even feasible to routinely find all the sub-groups that can be made of the items that exceed the threshold when their weights are summed.
What is the most efficient way to generate all sets that meet this criteria? What is complexity of this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个更有效的实施。值得注意的说明:
:
输出:这
意味着它可以解决25个元素的问题,其中解决方案包含约225毫秒的〜100K不同的子集。
我确实针对天真的解决方案进行了测试,以确保正确性。
关于时间复杂性,很难绑定它,因为它的运行时间确实取决于数字的值(或者是输出大小)。您可以将其绑定为
o(n log n + s * n)
其中n
是输入列表的大小,s
是输出列表的大小。Here's a more efficient implementation. Worthwhile notes:
Timing it:
Output:
Meaning its able to solve problems of 25 elements where the solution contains ~100k distinct subsets in about 225ms.
I did test this against a naive solution to ensure correctness as well.
Regarding time complexity, its hard to bound this, because its run time really depends on the value of the numbers (or rather, the output size). You could bound it as
O(n log n + s * n)
wheren
is the size of the input list, ands
is the size of the output list.您可以使用
itertools
生成子列表,然后根据条件过滤这些列表:时间复杂性很高,因为您首先需要生成每个长度的子清单(外部循环),然后根据条件(内部的循环)搜索正确的值,因此至少是O(n^2)。
You can use
itertools
to generate sub-lists and then filter those lists based on a condition:Time complexity is quite high as you first need to generate sub-lists of every length (outer for loop), then search right values based on condition (inner for loop), so it will be at least O(n^2).