diff --git a/combination sum.py b/combination sum.py new file mode 100644 index 00000000..a2964256 --- /dev/null +++ b/combination sum.py @@ -0,0 +1,21 @@ +class Solution: + def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: + self.result=[] + self.helper(candidates,target,0,[]) + return self.result + def helper(self,candidates,target,i,path): + + #basecase + if target<0 or i==len(candidates): + return + if target==0: + self.result.append(path) + return + + #nochoose case + self.helper(candidates,target,i+1,list(path)) + #choose case + path.append(candidates[i]) + self.helper(candidates,target-candidates[i],i,list(path)) + + \ No newline at end of file