Combination Sum II - 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.
Important constraints:
- Each number in
candidatesmay only be used once in the combination - The solution set must not contain duplicate combinations
Return the answer as a list of lists, where each inner list represents a valid combination.
Input & Output
Example 1 — Basic Case with Duplicates
$
Input:
candidates = [10,1,2,7,6,1,5], target = 8
›
Output:
[[1,1,6],[1,2,5],[1,7],[2,6]]
💡 Note:
After sorting [1,1,2,5,6,7,10], we find combinations: 1+1+6=8, 1+2+5=8, 1+7=8, and 2+6=8. Each number is used at most once per combination.
Example 2 — Multiple Duplicates
$
Input:
candidates = [2,5,2,1,2], target = 5
›
Output:
[[1,2,2],[5]]
💡 Note:
After sorting [1,2,2,2,5], valid combinations are: 1+2+2=5 (using first two 2s) and 5=5. The combination [2,2,1] would be duplicate of [1,2,2].
Example 3 — No Valid Combinations
$
Input:
candidates = [1,2], target = 4
›
Output:
[]
💡 Note:
No combination of [1,2] can sum to 4. Maximum possible sum is 1+2=3.
Constraints
- 1 ≤ candidates.length ≤ 100
- 1 ≤ candidates[i] ≤ 50
- 1 ≤ target ≤ 30
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array [10,1,2,7,6,1,5] with target 8
2
Process
Sort array and use backtracking to find combinations
3
Output
All unique combinations: [[1,1,6],[1,2,5],[1,7],[2,6]]
Key Takeaway
🎯 Key Insight: Sort first to group duplicates, then skip duplicates at the same recursion level to avoid duplicate combinations
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code