Abdul Kader’s Post

🚀 Day 41 of #100DaysOfCode — Leetcode + HackerRank Edition! Today’s challenge was a twist on yesterday’s recursive puzzle — this time with stricter rules and smarter pruning: 🧩 combinationSum2(candidates, target) — Find all unique combinations that sum to a target, using each number only once. 📌 Challenge: Given a list of positive integers, return all combinations that add up to a target. → Each number can be used once per combination → Combinations must be unique (no duplicates in different orders) → Example: candidates = [10,1,2,7,6,1,5], target = 8 ✅ Output: [[1,1,6],[1,2,5],[1,7],[2,6]] 🔍 Approach: → Sorted the input to handle duplicates cleanly → Used a recursive helper with backtracking → Skipped repeated values at the same recursive level → Moved to the next index after choosing a candidate (no reuse!) 💡 What made it click: → Realized that i > start and candidates[i] == candidates[i - 1] is the key to skipping duplicates → Practiced dry runs to see how [1,2,5] gets built and why [1,2,5] doesn’t repeat → Saw how index control + pruning = clean and efficient recursion → Appreciated how path.pop() lets us rewind and explore new paths 📚 What I learned: ✅ How to implement backtracking with recursion ✅ How to avoid duplicate combinations using sorted input and index checks ✅ How to prune invalid paths early for performance ✅ The elegance of recursive tree exploration Have you tackled this one before? Did you use recursion or dynamic programming? Let’s swap strategies 💬 #Day40 #Leetcode #Python #Backtracking #RecursionChallenge #LearnInPublic #CodeNewbie #TechJourney #100DaysOfCode #DSA

  • graphical user interface, text, application

To view or add a comment, sign in

Explore content categories