(target, currPath, index, candidates, paths)
| 67 | |
| 68 | // Time O(2^n) | Space O(n) |
| 69 | function find(target, currPath, index, candidates, paths) { |
| 70 | if (target === 0) { |
| 71 | paths.push(currPath.slice()); |
| 72 | return; |
| 73 | } else { |
| 74 | while (index < candidates.length && target - candidates[index] >= 0) { |
| 75 | find( |
| 76 | target - candidates[index], |
| 77 | [...currPath, candidates[index]], |
| 78 | index + 1, |
| 79 | candidates, |
| 80 | paths |
| 81 | ); |
| 82 | index++; |
| 83 | while (candidates[index - 1] === candidates[index]) index++; |
| 84 | } |
| 85 | } |
| 86 | } |