From c6a2964720e1808e2a2593283ac93c4359e40e11 Mon Sep 17 00:00:00 2001 From: Hemish Veeraboina <85383455+v-hemish@users.noreply.github.com> Date: Tue, 21 Oct 2025 17:33:28 -0400 Subject: [PATCH] Backtracking-1 --- problem1.py | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++ problem2.py | 43 ++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 problem1.py create mode 100644 problem2.py diff --git a/problem1.py b/problem1.py new file mode 100644 index 00000000..8e73e6c9 --- /dev/null +++ b/problem1.py @@ -0,0 +1,77 @@ +# Time Complexity: 2 ^ n +# Space Complexity: O(h) -> height of the stack +class Solution: + def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: + res = [] + def rec(i, target, path): + if target < 0 or i == len(candidates): + return + if target == 0: + res.append(path[:]) + return + + rec(i+1, target, path) + path.append(candidates[i]) + rec(i, target - candidates[i], path) + path.pop() + + rec(0, target, []) + return res + +""" +# Time Complexity: 2 ^ n +# Space Complexity: O(h) -> height of the stack +class Solution: + def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: + res = [] + def rec(pivot, target, path): + if target < 0 or pivot == len(candidates): + return + if target == 0: + res.append(path[:]) + return + + for i in range(pivot, len(candidates)): + # Action + path.append(candidates[i]) + + # Recurse + + rec(i, target - candidates[i], path) + + # Backtrack + path.pop() + + + rec(0, target, []) + return res + +""" +# Time Complexity: 2 ^ n +# Space Complexity: O(h) -> height of the stack +class Solution: + def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: + res = [] + def rec(pivot, target, path): + if target < 0 or pivot == len(candidates): + return + if target == 0: + res.append(path[:]) + return + + for i in range(pivot, len(candidates)): + # Action + path.append(candidates[i]) + + # Recurse + + rec(i, target - candidates[i], path) + + # Backtrack + path.pop() + + + rec(0, target, []) + return res + +""" diff --git a/problem2.py b/problem2.py new file mode 100644 index 00000000..103fc540 --- /dev/null +++ b/problem2.py @@ -0,0 +1,43 @@ +# Space Complexity: O(h) -> height of stack +# Time Complexity: 4 ^ len(num) +class Solution: + def addOperators(self, num: str, target: int) -> List[str]: + + res = [] + + def helper(num, pivot, calc, tail, path, target, res): + + if pivot == len(num): + if target == calc: + res.append(''.join(map(str, path))) + return + + for i in range(pivot, len(num)): + if num[pivot] == '0' and i!=pivot: continue + curr = int(num[pivot:i+1]) + if pivot == 0: + path.append(curr) + helper(num, i+1, curr, curr, path, target, res) + path.pop() + else: + # + + path.append('+') + path.append(curr) + helper(num, i+1, calc + curr, curr, path, target, res) + path.pop() + path.pop() + # - + path.append('-') + path.append(curr) + helper(num, i+1, calc - curr, -curr, path, target, res) + path.pop() + path.pop() + # * + path.append('*') + path.append(curr) + helper(num, i+1, calc - tail + tail * curr, tail*curr, path, target, res) + path.pop() + path.pop() + + helper(num, 0, 0, 0, [], target, res) + return res