Skip to content

Completed Backtracking-1 #1060

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
59 changes: 59 additions & 0 deletions addOperators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#TimeComplexity: O(4^n) where n is lenght of num .. here we have 4 choices at each idx, +,-,* or just concat with next number
#SpaceComplexity: O(k * n) k valid expressions
#approach :Similar to other backtracking fix pivot and then try diffrent combinations..
# also use tail approach to track prev used number to make * easy

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



def helper(num,pivot,path, cal, tail, target,result): #path at start will [] for backtracking solution
#base,, at pv== len, and res=target

if pivot==len(num) :
if cal==target:

result.append("".join(path))
return


#logic
for i in range(pivot,len(num)):
s=num[pivot:i+1]
if num[pivot]=="0" and len(s)>1:continue
curr=int(s)

if pivot==0:
#no need add operators
path.append(str(curr))
helper(num,i+1,path, curr, curr, target,result)
path.pop()
else:
#+
path.append("+")
path.append(str(curr))

helper(num,i+1,path, cal+curr, curr, target,result)
path.pop()
path.pop()
#-
path.append("-")
path.append(str(curr))

helper(num,i+1,path, cal-curr, -curr, target,result)
path.pop()
path.pop()
#*
path.append("*")
path.append(str(curr))

helper(num,i+1,path, cal-tail+curr*tail, curr*tail, target,result)
path.pop()
path.pop()

result=[]
helper(num,0,[], 0, 0, target,result)
return result


63 changes: 63 additions & 0 deletions combinationSum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#TimeComplexit: O(N ^ (T / min_val)) T is target , min_val in nums . every combination of numbers (with repetition allowed) that can sum to the target.
# In the worst case, each recursive call can branch into up to n calls.
#Space Complexity: O(T / min_val)
#App: Do a for loop based recursion with backtracking ..fix a pivot and then move to see if you find target sum if yes add to path
# else try different combiations with pivot fixed







class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
ans,n=[],len(candidates)

def helper(idx,target,path,c):
#base
if target<0 or idx==n:return
if target==0:ans.append([val for val in path]) #pass by refernce



#logic
#include
path.append(candidates[idx])

helper(idx,target-candidates[idx],path,c+1)

#backtrack
if c==0 and path:path.pop()
else:

while(c and path):
path.pop()
c-=1


#dont include
helper(idx+1,target,path,c)
helper(0,target,[],0)
return ans

################################################################################################################
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:

res=[]

def dfs(i, cur, total):
if total == target:
res.append(cur.copy())
return
if i>=len(candidates) or total > target:
return

cur.append(candidates[i])
dfs(i, cur, total + candidates[i])
cur.pop()
dfs(i+1, cur, total)

dfs(0, [], 0)
return res