gahogg/Leetcode-Solutions

Missing Combination Sum - Leetcode 39

vedangj-cmu opened this issue · 0 comments

I came across a Python solution for the Combination Sum problem that was missing. The code below works for that purpose.

res, sol = [], []

def backtrack():
    if sum(sol) == target:
        res.append(sol[:])
        return
    
    if sum(sol) > target:
        return
    
    for i in candidates:
        if len(sol) > 0 and i < sol[-1]:
            continue

        sol.append(i)
        backtrack()
        sol.pop()

backtrack()
return res