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
16 changes: 16 additions & 0 deletions Problem 1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
res = []
def helper(i, sub, cursum):
nonlocal res
if cursum > target or i >= len(candidates):
return
if cursum==target:
res.append(sub.copy())

for c in range(i, len(candidates)):
sub.append(candidates[c])
helper(c, sub, cursum+candidates[c])
sub.pop()
helper(0, [], 0)
return res
24 changes: 24 additions & 0 deletions Problem 2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution:
def addOperators(self, num: str, target: int) -> List[str]:
n = len(num)
res = []
def dfs(i,sofar, value, prev): #123
if i >= n and value == target:
# print(sofar, value)
res.append("".join(sofar))
return

for j in range(i, n):
if not sofar:
dfs(j+1, [num[i:j+1]], int(num[i:j+1]), int(num[i:j+1]))
else:
dfs(j+1, sofar + ['+'] + [num[i:j+1]], value+int(num[i:j+1]), int(num[i:j+1]) )
dfs(j+1, sofar + ['-'] + [num[i:j+1]], value-int(num[i:j+1]), -int(num[i:j+1]) )

#for *, bc of bodmas, need to have prev value so that we will add or minus ( opps of last operation), multiply it with current one and then add it back to value(which is prev res or res sofar)
dfs(j+1, sofar + ['*'] + [num[i:j+1]], value-prev + prev*int(num[i:j+1]), prev*int(num[i:j+1]))

if num[i]=='0': #so that it doesn't consider vals like 00, 05, 005 etc. hence after 1 0, don't dfs more
break
dfs(0, [], 0, None)
return res