Skip to content

Combination Sum

MediumBacktracking
Decode it first. Clues, constraints, pattern and plan before any code.

Given a list of distinct positive integers candidates and a target, return every unique combination of candidates that adds up to target. You may use the same candidate any number of times. Two combinations are the same if they use the same numbers the same number of times, regardless of order. Return the combinations in any order.

candidates = [2, 3, 6, 7], target = 7  →  [[2, 2, 3], [7]]
candidates = [2, 3, 5],    target = 8  →  [[2, 2, 2, 2], [2, 3, 3], [3, 5]]
candidates = [2],          target = 1  →  []

Constraints

  • 1 <= candidates.length <= 30
  • 1 <= candidates[i] <= 40
  • All candidates are distinct.
  • 1 <= target <= 40