How to solve Combination Sum problem with DFS on LeetCode

🧩 Day 36 — Combination Sum (LeetCode 39) 📝 Problem -Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. -The same number may be chosen from candidates an unlimited number of times. -Two combinations are unique if the frequency of at least one number is different. 🔁 Approach -Use backtracking (DFS) to explore all possible combinations. -At each step, choose a number and subtract it from the remaining target. -If the remaining target becomes 0, store the current combination. -If it becomes negative, backtrack (stop exploring that path). -To avoid duplicates, always start the next recursive call from the current index or later. -This ensures combinations like [2,3] and [3,2] are treated as the same. 📊 Complexity -Time Complexity: O(2ⁿ) -Space Complexity: O(n) 🔑 Concepts Practiced -Backtracking and recursion -Depth-first search (DFS) -Combination generation with repetition allowed -Control of duplicate combinations using start index #Leetcode #DSA #Python #DFS #Search

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories