Solved Leetcode's combinationSum with backtracking and recursion

🚀 Day 40 of #100DaysOfCode — Leetcode + HackerRank Edition! Today’s challenge was a recursive deep dive into combinatorial search using backtracking: 🧩 combinationSum(candidates, target) — Find all unique combinations that sum to a target. 📌 Challenge: Given a list of positive integers, return all combinations that add up to a target. → You can reuse numbers → Combinations must be unique (no duplicates in different orders) → Example: candidates = [2, 3, 6, 7], target = 7 ✅ Output: [[2, 2, 3], [7]] 🔍 Approach: → Used a recursive helper function with backtracking → Explored each candidate starting from a given index to avoid duplicates → Built combinations incrementally and tracked the running total → Pruned paths early if the total exceeded the target → Saved valid paths when the total matched the target 💡 What made it click: → Backtracking is like exploring a maze — try a path, backtrack if it overshoots, and save the ones that hit the goal → Realized the power of path.pop() to undo choices and explore alternatives → Practiced dry runs to visualize how [2, 2, 3] gets built step-by-step → Saw how recursion + pruning = efficient search 📚 What I learned: ✅ How to implement backtracking with recursion ✅ How to avoid duplicate combinations using index control ✅ How to prune invalid paths early for performance ✅ The beauty of recursive tree exploration Have you tried 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