I don't know if I describe the problem correctly. The problem is: there's the given number: 2, 3, 7, and let's say the target number is 10. Use these given numbers only, find the combination that add up to 10. For example: 2 + 2 + 2 + 2 + 2 = 10 or 3 + 7 = 10 or 7 + 3 = 10, etc.
Given that, I came up with the recursive solution (Using Javascript):
function combination(n) {
if(n < 2) {
return 0
}
if(n === 2) {
return 1
}
if(n === 3) {
return 1
}
if(n === 7) {
return 4
}
return combination(n - 2) + combination(n - 3) + combination(n - 7)
}
However, the time of this solution is exponentially growing. So I was trying to solve this by using DP (Dynamic Programming).But I can't think a way of it.
Any ideas ? I would be grateful if someone enlighten me.