Efficient algorithm to get the combinations of all items in object

前端 未结 4 1799
别跟我提以往
别跟我提以往 2021-01-05 02:01

Given an array or object with n keys, I need to find all combinations with length x.
Given X is variable. binomial_coefficient(n,x)

4条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-05 02:40

    And here's the true recursion.

    function seq(a,b){
      var res = [];
      for (var i=a; i<=b; i++)
        res.push(i);
      return res;
    }
    
    function f(n,k){
      if (k === 0)
        return [[]];
        
      if (n === k)
        return [seq(1,n)];
        
      let left = f(n - 1, k - 1),
          right = f(n - 1, k);
        
      for (let i=0; i

提交回复
热议问题