Skip to contentKEEP THE CRAFTTHE SANCTUARY

Reusable-Candidate Combination Sum

Find every combination of candidates that adds up to target, using each candidate as often as you like.

The problem

Implement Solution.combinationSum(candidates, target). candidates holds distinct positive integers, and you may use each one any number of times. Return every unique combination of candidates whose sum is target. Two combinations are the same if they use the same values the same number of times, so [2, 2, 3] and [3, 2, 2] count once. The combinations and the values inside them can be in any order. For example, candidates = [2, 3, 6, 7] with target = 7 gives [[2, 2, 3], [7]]. There are 1 to 30 candidates, each from 2 to 40, 1 <= target <= 40, and there are fewer than 150 answers.

Starter code

class Solution:
    def combinationSum(self, candidates, target):
        pass

Public examples

  1. candidates = [2, 3, 6, 7], target = 7gives[[2, 2, 3], [7]]
  2. candidates = [2], target = 4gives[[2, 2]]
  3. candidates = [7, 3], target = 7gives[[7]]

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.