找到满足特定条件的最大套数

时间:2017-12-29 11:19:37

标签: algorithm

给出一些数字:

  

156,434,600

如何查找超过500的最大组数?它允许您创建一个甚至只有一个元素的组。但是不允许在组之间重复使用元素。因此,在上面的例子中,答案是:

  

2 // {156,434},{600}

我希望当我得到另一组具有不同大小的数字时,通用算法可以应用它。

  • 156,434,600
  • 642,324,174,100,452
  • 174,100,455,900,1200,341

但我不知道我应该从哪里开始。所以,请帮助我一个好主意。

2 个答案:

答案 0 :(得分:0)

一种方法可能是对二进制数量的二进制搜索最佳解决方案,每次使用多项式时间近似方案进行多子集求和问题(例如https://www.researchgate.net/publication/262408639_The_Multiple_Subset_Sum_Problem 1 )。

<子> 1。多子集和问题; Alberto Caprara,Hans Kellerer和Ulrich Pferschy; SIAM期刊优化 卷。 11:问题。 2:页面。 308-319 (问题发布日期:2000年)

答案 1 :(得分:0)

我很确定这个问题是NP-Hard。我现在能想到的最接近的问题是bin packing problem,你想要最小化适合n个不同大小项目所需的箱子B(大小相等V)的数量。

在您的情况下,您希望最大化容器的数量,这些容器的最小尺寸为V.

作为近似,您可以调整垃圾箱包装问题的贪婪策略。

1) Sort your set of numbers in descending order. 

2) Place all numbers larger than 500 in a seperate group

3) Open a new empty group

4) Find the smallest number that increases the sum of the current open group over 500. 
     If such a number exists, put this number in the current open group jump to 3)
     If no such number exists select the largest number not yet used and place it in the open group and repeat 4)

示例:

174,100,455,900,1200,341

排序后

1200,900,455,341,174,100

结果组

{1200},{900},{455,100},{341,174}