Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing Combination Sum - Leetcode 39 #25

Open
vedangj-cmu opened this issue Oct 19, 2024 · 0 comments
Open

Missing Combination Sum - Leetcode 39 #25

vedangj-cmu opened this issue Oct 19, 2024 · 0 comments

Comments

@vedangj-cmu
Copy link

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant