From 1e056121a3fc6a5cada44405c026f4465e924149 Mon Sep 17 00:00:00 2001 From: Shinjanee Gupta Date: Wed, 11 Mar 2026 11:17:07 -0700 Subject: [PATCH] Backtracking-1 --- CombinationSum.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 CombinationSum.py diff --git a/CombinationSum.py b/CombinationSum.py new file mode 100644 index 00000000..27da196d --- /dev/null +++ b/CombinationSum.py @@ -0,0 +1,27 @@ +# Time Complexity : O(2 ^ (m+n)) where m is the candidates length and n is the target +# Space Complexity : O(n) +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No +# Approach : We try all combinations starting from a pivot index. +# The loop represents both choices: picking i handles the choose case, and moving i forward handles the no-choose. +# After choosing a number, we recurse with a reduced target and backtrack to explore other options. + +class Solution: + def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: + self.res = [] + self.helper(candidates, target, 0, []) + return self.res + + def helper(self, candidates, target, pivot, path): + if target < 0 or pivot == len(candidates): + return + + if target == 0: + self.res.append(list(path)) + return + + for i in range(pivot, len(candidates)): + path.append(candidates[i]) + self.helper(candidates, target - candidates[i], i, path) + path.pop() + \ No newline at end of file