Combination Sum II: Backtracking with Duplicate Handling

🔥 Day 96/100 of Code – Combination Sum II: Backtracking with Duplicate Handling! Today tackled a refined version of combination sum with no reuse and duplicate candidates — requiring careful pruning: ✅ Problem 40: Combination Sum II Task: Find all unique combinations (each candidate used once) that sum to target, with duplicates in input. Approach: Sort + backtracking with skip logic: Sort candidates to group duplicates At each step, iterate from ind to end: Skip duplicates: if (i > ind && nums[i] == nums[i-1]) continue Break early if nums[i] > target (pruning) Include nums[i], recurse with i+1 (no reuse) Backtrack Key Insight: Sorting lets us skip duplicate combinations elegantly. The condition i > ind ensures we skip duplicate values within the same recursion level but allow them across different levels. Complexity: O(2^n) worst-case, but pruning reduces search space significantly. A clean example of how sorting and careful duplicate skipping refine backtracking for real-world inputs! 🔄🔍 #100DaysOfCode #LeetCode #Java #Backtracking #DFS #Combinations #Algorithm #Day96

  • graphical user interface, text

To view or add a comment, sign in

Explore content categories