Combination Sum

Medium

Given an array of distinct integers candidates and an integer target, return every unique combination of numbers from candidates that adds up to target. You can return the combinations in any order, and the numbers inside each one in any order too.

You can use the same number as many times as you like. Two combinations are the same if they use every number the same number of times, so [2,2,3] and [3,2,2] are one combination and should only appear once.

The tests are chosen so that every input has fewer than 150 combinations.

Examples

Example 1

Input:  candidates = [2,3,6,7], target = 7
Output: [[2,2,3],[7]]
Example 1: target = 72232 + 2 + 3777

2 + 2 + 3 = 7 uses 2 twice, which is allowed. 7 on its own is also 7. No other combination adds up to 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: []

Every combination of 2s adds up to an even number, and the smallest is 2. Nothing adds up to 1.

Example 4

Input:  candidates = [3,5,8], target = 11
Output: [[3,3,5],[3,8]]
Example 4: order doesn't matter335[3,3,5]533[5,3,3]Same numbers, same combination: list it once.

[5,3,3] adds up to 11 too, but it uses the same numbers as [3,3,5], so it isn't a new combination.

Constraints

  • 1 <= candidates.length <= 30
  • 2 <= candidates[i] <= 40
  • All the numbers in candidates are different.
  • 1 <= target <= 40
TypeScript
Loading editor…
Test cases36
Case 1
candidates = [2,3,6,7]
target = 7
Case 2
candidates = [2,3,5]
target = 8
Case 3
candidates = [2]
target = 1
Case 4
candidates = [3,5,8]
target = 11
Case 5
candidates = [7,3,2]
target = 18
Case 6
candidates = [5]
target = 5
Case 7
candidates = [10]
target = 5
Case 8
candidates = [2]
target = 40
Case 9
candidates = [3]
target = 40
Case 10
candidates = [40]
target = 40
Case 11
candidates = [39,40]
target = 1
Case 12
candidates = [2,40]
target = 40
Case 13
candidates = [8,7,4,3]
target = 11
Case 14
candidates = [4,6,8]
target = 9
Case 15
candidates = [6,9,15]
target = 30
Case 16
candidates = [5,10,25]
target = 40
Case 17
candidates = [11,13,17,19]
target = 38
Case 18
candidates = [2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31]
target = 6
Case 19
candidates = [40,39,38,37,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11]
target = 40
Case 20
candidates = [2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31]
target = 1
Case 21
candidates = [2,3]
target = 40
Case 22
candidates = [3,4,5]
target = 40
Case 23
candidates = [12,21,30,9]
target = 39
Case 24
candidates = [2,5,7]
target = 40
Case 25
candidates = [13,14,15,16,17]
target = 31
Case 26
candidates = [8,10,6,3,4,2]
target = 15
Case 27
candidates = [2,3,5,7]
target = 40
Case 28
candidates = [11,7,5,3,2]
target = 40
Case 29
candidates = [3,5,7,9,11]
target = 40
Case 30
candidates = [2,3,6,7,9,10]
target = 30
Case 31
candidates = [24,20,34,5,28,25,12,15]
target = 32
Case 32
candidates = [7,32,37,14,11,16,6,39]
target = 21
Case 33
candidates = [11,2]
target = 2
Case 34
candidates = [28,26,30,33]
target = 28
Case 35
candidates = [28,8,34,33,7,12,37]
target = 20
Case 36
candidates = [7,9,35,27]
target = 4