【问题标题】:Partition of a set into K disjoint subsets with equal sum将一个集合划分为 K 个等和的不相交子集
【发布时间】:2014-12-05 18:55:30
【问题描述】:

给定 n ( n

【问题讨论】:

标签: algorithm partitioning


【解决方案1】:

一种改进蛮力方法的方法:

s = the sum of all the numbers
for i = 2 to 10
  k = s / i

  if k is an integer then
    Get all partitions of the input array with a minimum subset size of 2 elements and a maximum of n-1 elements.
    Check each partition as it's generated to see if all the subsets have the same sum.
    end if

  next i

硬(和慢)部分是生成分区。您可以使用递归函数来做到这一点。划分 20 个数字应该不会太慢。您可以通过尽早丢弃子集和不相等的分区来优化分区生成。

【讨论】:

  • 你能解释一下“如果 k 是一个整数,那么看看你是否可以制作 i 个子集,每个子​​集加起来为 k。”?我的意思是,如何找到涵盖所有 n 个数字且每个数字都相等的 i 不相交子集?
  • 这是蛮力部分,但由于您只需为几个值执行此操作,因此速度要快得多。我会在答案中解释。
  • 好的.. 等待解释。实际上,我很难实现必须找到总和相等的不相交子集的部分。
  • 你能帮我解决递归分区生成函数吗?我如何实现它?
猜你喜欢
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
  • 2016-11-28
  • 1970-01-01
  • 1970-01-01
  • 2012-07-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多