Question
Given an array of distinct integersÂ
candidates
 and a target integerÂtarget
, return a list of all unique combinations ofÂcandidates
 where the chosen numbers sum toÂtarget
. You may return the combinations in any order.The same number may be chosen fromÂ
candidates
 an 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Â
target
 is less thanÂ150
 combinations for the given input.
This is abacktracking 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