Horje
Combinatorial summation problem Code Example
Combinatorial summation problem
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 ] ]




Javascript

Related
vanilla JS Code Example vanilla JS Code Example
storage package npm react Code Example storage package npm react Code Example
how to make color squares in angular Code Example how to make color squares in angular Code Example
lesson-3 Code Example lesson-3 Code Example
world biggest company in terms of revenue Code Example world biggest company in terms of revenue Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
7