diff --git a/combination_sum.py b/combination_sum.py new file mode 100644 index 00000000..85467679 --- /dev/null +++ b/combination_sum.py @@ -0,0 +1,33 @@ +# https://leetcode.com/problems/combination-sum/ + +#Time Complexity- O(n*2^(m+n)) Self Complexity- O(n2) + +class Solution: + def combinationSum(self, candidates, target): + # initialise the result set to store the combinations + self.result = [] + # call the helper function + self.helper(candidates, target, 0, []) + return self.result + + def helper(self, candidates, target, pivot, path): + # base case 1: when the sum is bigger than target + if target < 0: + return + # base case 2: when amount become zero add the path to result + if target == 0: + # append that path to result + self.result.append(list(path)) + return + # use the for loop to choose the combination + # for first iteration the pivot will be at 0th index + for i in range(pivot, len(candidates)): + # create a new list + new_path = list(path) + # add the element to that list + new_path.append(candidates[i]) + # recurse if the target is still not achieved return to for loop + self.helper(candidates, target - candidates[i], i, new_path) + +solution = Solution() +print(solution.combinationSum([2,3,6,7],7)) \ No newline at end of file diff --git a/expression_add_operators.py b/expression_add_operators.py new file mode 100644 index 00000000..62b0d365 --- /dev/null +++ b/expression_add_operators.py @@ -0,0 +1,42 @@ +# https://leetcode.com/problems/expression-add-operators/ + +class Solution: + def addOperators(self, num: str, target: int) -> list[str]: + self.result = [] + + # call the helper function + def helper(num, target, pivot, calc, tail, path): + # base case + if pivot == len(num): + # calc represent the current value of the calculation so far + # if the target is achieved + if calc == target: + # append that path into the result + self.result.append(path) + # use the for loop to choose the combination + for i in range(pivot, len(num)): + # if the number is starting with zero break the loop + if num[pivot] == '0' and i != pivot: + break + # find the current number + curr = int(num[pivot:i+1]) + + # if we are at the beginning of the string, we cannot add an operator before it + if pivot == 0: + # start the expression with the current number and continue recursion + helper(num, target, i+1, curr, curr, path + str(curr)) + # when the starting point is not 0, then add operators + else: + # for addition- add the current number to the expression + helper(num, target, i+1, calc + curr, curr, path + "+" + str(curr)) + # for substraction- substract the current number from the expression + helper(num, target, i+1, calc - curr, -curr, path + "-" + str(curr)) + # for multiplication- multiply the last number (tail) with the current number + helper(num, target, i+1, calc - tail + (tail * curr), tail * curr, path + "*" + str(curr)) + + # start the recursion with initial values + helper(num, target, 0, 0, 0, "") + return self.result + +solution = Solution() +print(solution.addOperators("123",6)) \ No newline at end of file