Problem sumowania kombinatorycznego
var targetArray = function(nums, target) {
var res = [];
var dfs = function(path,start) {
if(sum(path) === target) { //(1)
res.push([...path]);
return;
}
if(sum(path) > target) {
return;
}
for(let i=start; i<nums.length; i++) { //(2)
path.push(nums[i]); //(3)
dfs(path.slice(), i);
path.pop();
}
}
dfs([],0)
return res;
}
function sum(arr) {
if(arr.length===0)
return 0;
return arr.reduce((a,b)=>a+b)
}
console.log(targetArray([2,3,5],8))
// [ [ 2, 2, 2, 2 ], [ 2, 3, 3 ], [ 3, 5 ] ]