Question
Given an array of distinct integers
candidatesand a target integertarget, return a list of all unique combinations ofcandidateswhere the chosen numbers sum totarget. You may return the combinations in any order.The same number may be chosen from
candidatesan unlimited number of times. Two combinations are unique if the frequency of at least one of the chosen numbers is different.The test cases are generated such that the number of unique combinations that sum up to
targetis less than150combinations for the given input.
This is a backtracking question.
Idea
- Uses a Backtracking recursion tree (dfs)
- We want each branch to be unique, to do so, our binary decision is:
-
- We can include n amounts of the current number
-
- We cannot include any amounts of the current number
- Ex. A branch could include n 2’s and the other branch would include no 2’s

-
- Our base case is when our current sum is > target or if our current sum = target
- If our current sum reaches target, we append a copy of our current combination to our result
- We want to keep track of our current combination as we go down the tree
- Time complexity is