Skip to content
Decoder

Combination Sum

Medium

Problem

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.

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 <= candidates.length <= 30
  • 1 <= candidates[i] <= 40
  • All candidates are distinct.
  • 1 <= target <= 40

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.