Skip to content

Missing Combination Sum - Leetcode 39 #25

Open
@vedangj-cmu

Description

@vedangj-cmu

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions