Solution to the Subset sum problem, using dynamic programming with a bottom-up approach.
let numbers:Array<Array<Array<number>>> = []
for (let i = 0; i <= sum; i++) {
numbers.push([])
}
numbers[0].push([])
subset.forEach(number => {
for (let target = number; target <= sum; target++) {
numbers[target - number]?.forEach(combination => {
if (!combination.includes(number)) {
numbers[target].push([...combination, number])
}
})
}
})
let solution = [...(new Set(numbers[sum]))]
8
10
7
2
14
5
4
10
10
8
2
0
1
2
3
4
5
6
7
8
9
10