kiinlam
7/7/2014 - 6:59 AM

dp组合算法

dp组合算法

/*
 * @ref: http://lifesinger.googlecode.com/svn/trunk/lab/2009/combine-test.html
 * dp组合算法
 */
function dp_combine_yy(a, m) {
    var t = [[]], r = [];
    for (var i = 0, n = a.length; i < n; i++) {
        for (var j = 0, k = t.length; j < k; j++) {
            var s = t[j].concat([a[i]]);
            s.length < m ? t.push(s) : r.push(s);
        }
    }
    return r;
}
dp_combine_yy([1,2,3,4,6],3);