简体   繁体   English

给定一组n个整数,返回总和为0的k个元素的所有子集

[英]given a set of n integers, return all subsets of k elements that sum to 0

given a unsorted set of n integers, return all subsets of size k (ie each set has k unique elements) that sum to 0. 给定一组未排序的n整数,返回大小为k的所有子集(即每个集合具有k个唯一元素),其总和为0。

So I gave the interviewer the following solution ( which I studied on GeekViewpoint ). 所以我给了面试官以下解决方案(我在GeekViewpoint上学习 )。 No extra space used, everything is done in place, etc. But of course the cost is a high time complexity of O(n^k) where k=tuple in the solution. 没有使用额外的空间,一切都在适当的地方完成等等。但是当然成本是O(n ^ k)的高时间复杂度,其中k=tuple解决方案中的k=tuple

public void zeroSumTripplets(int[] A, int tuple, int sum) {
  int[] index = new int[tuple];
  for (int i = 0; i < tuple; i++)
    index[i] = i;
  int total = combinationSize(A.length, tuple);
  for (int i = 0; i < total; i++) {
    if (0 != i)
      nextCombination(index, A.length, tuple);
    printMatch(A, Arrays.copyOf(index, tuple), sum);
  }// for
}// zeroSumTripplets(int[], int, int)

private void printMatch(int[] A, int[] ndx, int sum) {
  int calc = 0;
  for (int i = 0; i < ndx.length; i++)
    calc += A[ndx[i]];
  if (calc == sum) {
    Integer[] t = new Integer[ndx.length];
    for (int i = 0; i < ndx.length; i++)
      t[i] = A[ndx[i]];
    System.out.println(Arrays.toString(t));
  }// if
}// printMatch(int[], int[], int)

But then she imposed the following requirements: 但后来她强加了以下要求:

  • must use hashmap in answer so to reduce time complexity 必须使用hashmap来减少时间复杂度
  • Must absolutely -- ABSOLUTELY -- provide time complexity for general case 必须绝对 - 绝对 - 为一般情况提供时间复杂性
  • hint when k=6, O(n^3) 当k = 6时提示,O(n ^ 3)

She was more interested in time-complexity more than anything else. 她对时间复杂性的兴趣比什么都重要。

Does anyone know a solution that would satisfy the new constraints? 有谁知道满足新约束的解决方案?


EDIT: 编辑:

Supposedly, in the correct solution, the map is to store the elements of the input and the map is then to be used as a look up table just as in the case for k=2 . 据推测,在正确的解决方案中,地图将存储输入的元素,然后地图将用作查找表,就像k=2

When the size of the subset is 2 (ie k=2 ), the answer is trivial: loop through and load all the elements into a map. 当子集的大小为2(即k=2 )时,答案是微不足道的:循环并将所有元素加载到地图中。 Then loop through the inputs again this time searching the map for sum - input[i] where i is the index from 0 to n-1 , which would then be the answers. 然后再次遍历输入,这次搜索地图的sum - input[i] where i is the index from 0 to n-1 ,然后是答案。 Supposedly this trivial case can be extended to where k is anything. 据说这个简单的案例可以扩展到k是什么的地方。

Since no-one else has made an attempt, I might as well throw in at least a partial solution. 由于没有其他人做过尝试,我不妨投入至少部分解决方案。 As I pointed out in an earlier comment, this problem is a variant of the subset sum problem and I have relied heavily on documented approaches to that problem in developing this solution. 正如我在之前的评论中指出的那样,这个问题是子集求和问题的变体,我在开发这个解决方案时严重依赖于记录的方法来解决这个问题。

We're trying to write a function subsetsWithSum(A, k, s) that computes all the k-length subsets of A that sum to s. 我们正在尝试编写一个函数subsetsWithSum(A, k, s)来计算总和为s的所有k长度的A子集。 This problem lends itself to a recursive solution in two ways: 这个问题有两个方面的递归解决方案:

  1. The solution of subsetsWithSum(x 1 ... x n , k, s) can be found by computing subsetsWithSum(x 2 ... x n , k, s) and adding all the valid subsets (if any) that include x 1 ; subsetsWithSum(x 1 ... x n ,k,s)的解可以通过计算subsetsWithSum(x 2 ... x n ,k,s)并添加包含x 1的所有有效子集(如果有的话)来找到; and
  2. All the valid subsets that include element x i can be found by computing subsetsWithSum(A - x i , k-1, sx i ) and adding x i to each subset (if any) that results. 可以通过计算subsetsWithSum(A-x i ,k-1,sx i )并将x i添加到结果的每个子集(如果有的话)来找到包括元素x i的所有有效子集。

The base case for the recursion occurs when k is 1, in which case the solution to subsetsWithSum(A, 1, s) is the set of all single element subsets where that element is equal to s. 当k为1时发生递归的基本情况,在这种情况下,subsetsWithSum(A,1,s)的解是所有单个元素子集的集合,其中该元素等于s。

So a first stab at a solution would be 因此,首先要解决一个问题

/**
 * Return all k-length subsets of A starting at offset o that sum to s.
 * @param A - an unordered list of integers.
 * @param k - the length of the subsets to find.
 * @param s - the sum of the subsets to find.
 * @param o - the offset in A at which to search.
 * @return A list of k-length subsets of A that sum to s.
 */
public static List<List<Integer>> subsetsWithSum(
        List<Integer> A,
        int k,
        int s,
        int o)
{
    List<List<Integer>> results = new LinkedList<List<Integer>>();

    if (k == 1)
    {
        if (A.get(o) == s)
            results.add(Arrays.asList(o));
    }
    else
    {
        for (List<Integer> sub : subsetsWithSum(A, k-1, s-A.get(o), o+1))
        {
            List<Integer> newSub = new LinkedList<Integer>(sub);
            newSub.add(0, o);
            results.add(0, newSub);
        }
    }

    if (o < A.size() - k)
        results.addAll(subsetsWithSum(A, k, s, o+1));

    return results;
}

Now, notice that this solution will often call subsetsWithSum(...) with the same set of arguments that it has been called with before. 现在,请注意,此解决方案通常会使用之前调用过的相同参数集调用subsetsWithSum(...)。 Hence, subsetsWithSum is just begging to be memoized . 因此,subsetsWithSum只是乞求备忘

To memoize the function, I've put the arguments k, s and o into a three element list which will be the key to a map from these arguments to a result computed earlier (if there is one): 为了记住这个函数,我把参数k,s和o放到一个三元素列表中,它将是从这些参数到先前计算的结果(如果有的话)的映射的关键:

public static List<List<Integer>> subsetsWithSum(
        List<Integer> A,
        List<Integer> args,
        Map<List<Integer>, List<List<Integer>>> cache)
{
    if (cache.containsKey(args))
        return cache.get(args);

    int k = args.get(0), s = args.get(1), o = args.get(2);
    List<List<Integer>> results = new LinkedList<List<Integer>>();

    if (k == 1)
    {
        if (A.get(o) == s)
            results.add(Arrays.asList(o));
    }
    else
    {
        List<Integer> newArgs = Arrays.asList(k-1, s-A.get(o), o+1);

        for (List<Integer> sub : subsetsWithSum(A, newArgs, cache))
        {
            List<Integer> newSub = new LinkedList<Integer>(sub);
            newSub.add(0, o);
            results.add(0, newSub);
        }
    }

    if (o < A.size() - k)
        results.addAll(subsetsWithSum(A, Arrays.asList(k, s, o+1), cache));

    cache.put(args, results);
    return results;
}

To use the subsetsWithSum function to compute all the k-length subsets that sum to zero, one can use the following function: 要使用subsetsWithSum函数计算总和为零的所有k长度子集,可以使用以下函数:

public static List<List<Integer>> subsetsWithZeroSum(List<Integer> A, int k)
{
    Map<List<Integer>, List<List<Integer>>> cache =
            new HashMap<List<Integer>, List<List<Integer>>> ();
    return subsetsWithSum(A, Arrays.asList(k, 0, 0), cache);
}

Regrettably my complexity calculating skills are a bit (read: very) rusty, so hopefully someone else can help us compute the time complexity of this solution, but it should be an improvement on the brute-force approach. 令人遗憾的是,我的复杂性计算技能有点(读:非常)生锈,所以希望其他人可以帮助我们计算这个解决方案的时间复杂度,但它应该是对蛮力方法的改进。

Edit: Just for clarity, note that the first solution above should be equivalent in time complexity to a brute-force approach. 编辑:为了清楚起见,请注意上面的第一个解决方案应该在时间复杂度上与蛮力方法相当。 Memoizing the function should help in many cases, but in the worst case the cache will never contain a useful result and the time complexity will then be the same as the first solution. 在许多情况下,记住函数应该有所帮助,但在最坏的情况下,缓存永远不会包含有用的结果,时间复杂度将与第一个解决方案相同。 Note also that the subset-sum problem is NP-complete meaning that any solution has an exponential time complexity. 还要注意,子集和问题是NP完全的,这意味着任何解决方案都具有指数时间复杂度。 End Edit. 结束编辑。

Just for completeness, I tested this with: 为了完整起见,我测试了以下内容:

public static void main(String[] args) {
    List<Integer> data = Arrays.asList(9, 1, -3, -7, 5, -11);

    for (List<Integer> sub : subsetsWithZeroSum(data, 4))
    {
        for (int i : sub)
        {
            System.out.print(data.get(i));
            System.out.print(" ");
        }

        System.out.println();
    }
}

and it printed: 并打印:

9 -3 5 -11
9 1 -3 -7

I think your answer was very close to what they were looking for, but you can improve the complexity by noticing that any subset of size k can be thought of as two subsets of size k/2 . 我认为你的答案非常接近他们所寻找的,但你可以通过注意到任何大小为k子集可以被认为是两个大小为k/2子集来提高复杂性。 So instead of finding all subsets of size k (which takes O(n^k) assuming k is small), use your code to find all subsets of size k/2 , and put each subset in a hashtable, with its sum as the key. 因此,不是找到大小为k所有子集(假设k很小,取O(n^k) ),使用您的代码查找大小为k/2所有子集,并将每个子集放在一个哈希表中,其总和为键。

Then iterate through each subset of size k/2 with a positive sum (call the sum S ) and check the hashtable for a subset whose sum is -S . 然后用正和(遍历和S )迭代大小为k/2每个子集,并检查散列表中的和为-S的子集。 If there is one then the combination of the two subsets of size k/2 is a subset of size k whose sum is zero. 如果有一个大小然后的两个子集的组合k/2是大小的子集k其总和是零。

So in the case of k=6 that they gave, you would find all subsets of size 3 and compute their sums (this will take O(n^3) time). 因此,在他们给出的k=6的情况下,你会发现大小为3所有子集并计算它们的总和(这将花费O(n^3)时间)。 Then checking the hashtable will take O(1) time for each subset, so the total time is O(n^3) . 然后检查哈希表将为每个子集花费O(1)时间,因此总时间为O(n^3) In general this approach will take O(n^(k/2)) assuming k is small, and you can generalize it for odd values of k by taking subsets of size floor(k/2) and floor(k/2)+1 . 一般来说,这种方法将采用O(n^(k/2))假设k很小,并且你可以通过取大小floor(k/2)floor(k/2)+1子集来推广它的奇数值k floor(k/2)+1

@kasavbere - @kasavbere -

Recently a friend had one of those harrowing all-day interviews for a C++ programming job with Google. 最近,一位朋友对谷歌的C ++编程工作进行了全天采访。 His experience was similar to yours. 他的经历与你的相似。

It inspired him to write this article - I think you might enjoy it: 这激发了他写这篇文章 - 我想你可能会喜欢它:

The Pragmatic Defense 务实的防御

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

相关问题 给定整数集的子集,其和为常数 N:Java - Subsets of a given Set of Integers whose sum is a Constant N : Java 如何在Java中以递归方式从N元素集生成所有k元素子集 - How to Generate all k-elements subsets from an N-elements set recursively in Java 查找总计为n的集合的所有子集 - Find all subsets of a set that sum up to n 查找给定大小为n的大小为k的子集 - Finding subsets of size k for a given set of size n 给定一个数组,找到总和为值k的所有子集 - Given an array, find all subsets which sum to value k 给定每个列表中最多N个元素的K个排序列表,请对所有项目返回一个排序的迭代器 - Given K sorted lists of up to N elements in each list, return a sorted iterator over all the items 给定一个由 N 个整数组成的数组 A,返回最大的整数 K&gt;0,使得数组 A 中同时存在 K 和 -K(相反的数) - given an array A of N integers, return the largest integer K>0 such that both K and -K(the opposite number) exist in array A 给定整数数组,找到所有“最大”子集 - Given an array of integers find all “maximal” subsets 如何从java中的一组大小n迭代生成k个元素子集? - How to iteratively generate k elements subsets from a set of size n in java? 返回所有整数的和 - return the sum of all integers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM