Combination Sum II
Backtracking
Problem
Given a collection of candidate numbers (candidates
) and a target number (target
), find all unique combinations in candidates
where the candidate numbers sum to target
.
Each number in candidates
may only be used once in the combination.
Note: The solution set must not contain duplicate combinations.
For example:
Thought Process
Since some elements can be repeated in the input array, we can handle the case of duplicate combinations occuring due to repeated elements by sorting the input list and in our for loop we will check if candidates[i] == candidates[i-1] and continue if this is true because since we already came across that same element previously we don't want to do it again
Solution
Key Points
Need to sort the input list in order to check for duplicates
Same approach as Combination Sum I where are target dictates when we begin to backtrack
Time Complexity
Last updated
Was this helpful?