leetcode_gym/_problems/combination_sum.md
2026-08-25 19:14:07 -06:00

2.9 KiB

layout, title, last_solved, category, leetcode_url, status
layout title last_solved category leetcode_url status
page Combination Sum 2020-06-27 permutations https://leetcode.com/problems/combination-sum/ Attempted

Problem

Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.

The same repeated number may be chosen from candidates unlimited number of times.

Note:

All numbers (including target) will be positive integers.
The solution set must not contain duplicate combinations.
Example 1:

Input: candidates = [2,3,6,7], target = 7,
A solution set is:
[
  [7],
  [2,2,3]
]
Example 2:

Input: candidates = [2,3,5], target = 8,
A solution set is:
[
  [2,2,2,2],
  [2,3,3],
  [3,5]
]

Solution

{% highlight python %}

candidates = [2,3,6,7] target = 7

def combinationSum(candidates, target):

def backtrack(currentList, nums, start):
    
    if sum(currentList)==target:
        allValidCombos.append( currentList.copy() )
        return 
    if sum(currentList) > target: # optimization
        return
    for i in range(start, len(nums)):
        backtrack(currentList + [nums[i]], nums, i)
        # currentList.append( nums[i] )
        # backtrack( currentList, nums, start)

allValidCombos = []
backtrack([], candidates, 0 )
return allValidCombos

print( combinationSum(candidates, target) )

{% endhighlight %}


{% highlight python %}

def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
    
    output = []
    
    def backtrack(currentList, start):
        
        if sum(currentList)==target:
            output.append(currentList.copy())
            return
        
        if sum(currentList) > target: # optimization
            return
        
        for i in range(start, len(candidates)):
            backtrack(currentList + [candidates[i]], i)
        
    
    backtrack([], 0)
    return output

{% endhighlight %}


{% highlight python %}

def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
    
    output = []
    
    def backtrack(currentList, remaining, start):
    
        if remaining<0:
            return
        elif remaining==0:
            output.append(currentList.copy())
        else:
        
            for i in range(start, len(candidates)):
                currentList.append(candidates[i])
                backtrack(currentList, remaining-candidates[i], i)
                currentList.pop()

    backtrack([], target, 0)
    return output

{% endhighlight %}

image1