Combination Sum (Unique)

PHOTO EMBED

Thu Mar 10 2022 02:32:24 GMT+0000 (Coordinated Universal Time)

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  # backtracking 
        for i in range(index, len(candidates)):
            if candidates[i] == candidates[i-1]:
                continue
            dfs(target-candidates[i], i+1, 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