Generate subsets of length n

后端 未结 1 1542
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-26 08:51

Given a Set, generate all subsets of length n.

  • Sample input:

    set = new Set([\'a\', \'b\', \'c\']), length = 2
    
  • Sample

1条回答
  •  北恋
    北恋 (楼主)
    2021-01-26 09:18

    I implemented your idea of deleting and reinserting values with ordered multi subsets:

    function orderedMultiSubsets(set, n) {
      if(!Number.isInteger(n) || n < 0) return function*(){}();
      var subset = new Array(n),
          iterator = set.values();
      return (function* backtrack(index) {
        if(index === n) {
          yield subset.slice();
        } else {
          for(var i=0; i

    And then I think I managed to prune as early as possible for the unordered subsets case:

    function subsets(set, n) {
      if(!Number.isInteger(n) || n < 0 || n > set.size) return function*(){}();
      var subset = new Array(n),
          iterator = set.values();
      return (function* backtrack(index, remaining) {
        if(index === n) {
          yield subset.slice();
        } else {
          for(var i=0; i

    I still use an array for the subsets, but its size is only n. So in case the size of the set is much bigger than n, this approach might use less memory than duplicating the set into an array, which I guess is the point of your question. But note that the deletions and insertions are probably more expensive than array lookups.

    Bonus: at the end, the order of the set is the same as before calling the function.

    0 讨论(0)
提交回复
热议问题