Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions combination-sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#--------Solution 1 : Backtraking------------
''' Time Complexity : O(2^(m+n))
Space Complexity : O(m+n)
Did this code successfully run on Leetcode : Yes
Any problem you faced while coding this : No
'''

class Solution:
def __init__(self):
self.result = []

def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
path = []
def helper(candidates, i, path, target):
#base
if i >= len(candidates) or target < 0:
return
if target == 0:
self.result.append(list(path))
return
#logic
#action
path.append(candidates[i])
#choose
helper(candidates, i, path, target-candidates[i])
#backtrack
path.pop()
#no Choose
helper(candidates, i+1, path, target)

helper(candidates,0,path,target)
return self.result


#--------Solution 2 : Creating new path each time------------
''' Time Complexity : O(n*2^(m+n))
Space Complexity : O(n*h)
Did this code successfully run on Leetcode : Yes
Any problem you faced while coding this : No
'''
class Solution:
def __init__(self):
self.result = []

def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
path = []
def helper(candidates, i, path, target):
#base
if i >= len(candidates) or target < 0:
return
if target == 0:
self.result.append(list(path))
return
#logic
#action
new_p = deepcopy(path)
new_p.append(candidates[i])
#choose
helper(candidates, i, new_p, target-candidates[i])
#no Choose
helper(candidates, i+1, path, target)

helper(candidates,0,path,target)
return self.result

#--------Solution 3 : For loop based recursion with backtracking------------
''' Time Complexity : O(2^(m+n))
Space Complexity : O(m+n)
Did this code successfully run on Leetcode : Yes
Any problem you faced while coding this : No
'''
class Solution:
def __init__(self):
self.result = []

def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
path = []
def helper(candidates, pivot, path, target):
#base
if target < 0:
return
if target == 0:
self.result.append(list(path))
return
#logic
#action
for i in range(pivot,len(candidates)):
path.append(candidates[i])
#choose
helper(candidates, i, path, target-candidates[i])
#backtrack
path.pop()


helper(candidates,0,path,target)
return self.result
88 changes: 88 additions & 0 deletions expression-add-operator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#--------Solution 1 : FOr loop recursion - creating new path for each calll------------
''' Time Complexity : O(4^n * n) : n for string concatenation at each call
Space Complexity : O(n)
Did this code successfully run on Leetcode : Yes
Any problem you faced while coding this : No
'''

class Solution:
def addOperators(self, num: str, target: int) -> List[str]:
self.result = []
self.target = target

def helper(num, pivot, calc, tail, path):
#base
if pivot == len(num):
if calc == self.target:
self.result.append(path)
#logic
for i in range(pivot, len(num)):
if num[pivot] == '0' and pivot!=i:
break

curr = int(num[pivot:i+1])
if pivot == 0:
helper(num, i+1, curr, curr, path + str(curr))
else:
# for +
helper(num, i+1, calc + curr, curr, path + "+" + str(curr))
#for -
helper(num, i+1, calc - curr, -curr, path + "-" + str(curr))
#for *
helper(num, i+1, calc - tail + (tail * curr) , tail * curr, path + "*" + str(curr))

helper(num, 0, 0, 0, "")
return self.result


#--------Solution 2 : For loop with Backtraking------------
''' Time Complexity : O(4^n)
Space Complexity : O(n)
Did this code successfully run on Leetcode : Yes
Any problem you faced while coding this : No
'''

class Solution:
def addOperators(self, num: str, target: int) -> List[str]:
self.result = []
self.target = target

def helper(num, pivot, calc, tail, path):
#base
if pivot == len(num):
if calc == self.target:
self.result.append("".join(path))
return
#logic
for i in range(pivot, len(num)):
if num[pivot] == '0' and pivot!=i:
break

curr = int(num[pivot:i+1])
le = len(path)
if pivot == 0:
path.append(str(curr))
helper(num, i+1, curr, curr, path)
path.pop()
else:
# for +
path.append('+')
path.append(str(curr))
helper(num, i+1, calc + curr, curr, path)
path.pop()
path.pop()
#for -
path.append('-')
path.append(str(curr))
helper(num, i+1, calc - curr, -curr, path)
path.pop()
path.pop()
#for *
path.append('*')
path.append(str(curr))
helper(num, i+1, calc - tail + (tail * curr) , tail * curr, path)
path.pop()
path.pop()

helper(num, 0, 0, 0, [])
return self.result