Combination Sum

Backtracking, problem 5 of 7

Combination Sum

Medium

LC #39

combinationspruning

Not attempted yet

You're given a list of distinct positive integers candidates and a positive target. Return every unique combination of candidates that adds up to target. The same candidate may be used any number of times.

Two combinations are the same if they use each number the same number of times ([2,2,3] and [3,2,2] count once). Return them in any order.

Example 1

Input: candidates = [2,3,6,7], target = 7
Output: [[2,2,3],[7]]

Example 2

Input: candidates = [2,3,5], target = 8
Output: [[2,2,2,2],[2,3,3],[3,5]]

Example 3

Input: candidates = [2], target = 1
Output: []

Constraints

  • 1 ≤ len(candidates) ≤ 30
  • 2 ≤ candidates[i] ≤ 40, all distinct
  • 1 ≤ target ≤ 40

Python

Loading draft…

Test results

8 tests available

No results yet

Run tests your code against the examples; Submit runs the hidden tests too.

3 examples, 5 hidden

Run examples, then submit all tests.