Combination Sum (Not Unique)

PHOTO EMBED

Thu Mar 10 2022 02:31:32 GMT+0000 (UTC)

Saved by @vijuhiremath #python #template #combinations #sum

def combinationSum2(candidates, target):
    res = []
    candidates.sort()
    
    def dfs(target, index, path):
        if target < 0:
            return  # backtracking
        if target == 0:
            res.append(path)
            return 
        for i in range(index, len(candidates)):
            dfs(target-candidates[i], i, path+[candidates[i]])
            
    dfs(target, 0, [])
    return res
content_copyCOPY

https://leetcode.com/problems/combination-sum/discuss/429538/General-Backtracking-questions-solutions-in-Python-for-reference-%3A