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
37 changes: 37 additions & 0 deletions combinationsum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Time Complexity : O(N) where N is the size of the list of candidates
# Space Complexity : O(N) where N is the size of the recursion stack in the worst case
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No

# Your code here along with comments explaining your approach:
# I am keeping an empty list to store the final answer.
# I am using a helper function to perform backtracking.
# The helper function takes the remaining target, current path and pivot index as input.
# If the target is less than 0, I return as it is not a valid combination.
# If the target is 0, I append the current path to the answer list as it is a valid combination.
# I iterate through the candidates starting from the pivot index to avoid duplicates.
# For each candidate, I append it to the current path and call the helper function recursively with
# the updated target (target - candidate), current path and the same pivot index (as we can reuse the same element).
# After the recursive call, I pop the last element from the current path to backtrack and try the next candidate.
# Finally, I return the answer list.



from typing import List
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
ans = []
def helper(target,path,pivot):
if target < 0:
return
if target == 0:
ans.append(list(path))
return
for i in range(pivot,len(candidates)):
path.append(candidates[i])
helper(target-candidates[i],path,i)
path.pop()
helper(target,[],0)
return ans


44 changes: 44 additions & 0 deletions expressions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Time Complexity : O(2^N.N) where N is the number of nodes in the tree
# Space Complexity : O(N) where N is the height of the tree
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No

# Your code here along with comments explaining your approach:
# I am creating an ans list to store the valid expressions.
# I am using a helper function to perform backtracking.
# The helper function takes the pivot index, tail value, calculated value and current path as input.
# If the pivot index is equal to the length of the num string, I check if the calculated value is equal to the target.
# If it is, I append the current path to the ans list.
# I iterate through the num string starting from the pivot index to avoid duplicates.
# For each character, I check if it is '0' and if it is not the first character in the current substring.
# If it is, I break the loop to avoid leading zeros.
# I convert the current substring to an integer and store it in curr.
# If the pivot index is 0, I call the helper function recursively with the updated pivot index, tail value, calculated value and current path.
# If the pivot index is not 0, I call the helper function recursively for each of the 3 operators (+, -, *) with the updated values.
# Finally, I return the ans list.


from typing import List
class Solution:
def addOperators(self, num: str, target: int) -> List[str]:
ans = []
def helper(pivot,tail,calc,path):
if pivot == len(num):
if target == calc:
ans.append(path)
for i in range(pivot,len(num)):
if num[pivot] == '0' and i != pivot:
break
curr = int(num[pivot:i+1])
if pivot == 0:
helper(i+1,curr,curr,path+str(curr))
else:
helper(i+1,curr,calc+curr,path+"+"+str(curr))

helper(i+1,-curr,calc-curr,path+"-"+str(curr))

helper(i+1,tail*curr,calc-tail+(curr*tail),path+"*"+str(curr))
helper(0,0,0,"")
return ans