Combination Sum

Medium

Given distinct candidates and a target, return every unique combination that sums to target.

The same candidate may be chosen an unlimited number of times, and the answer may be in any order.

Example 1

combinationSum()
Input
candidates = [2,3,6,7], target = 7
Output
[[2,2,3],[7]]

Example 2

combinationSum()
Input
candidates = [2,3,5], target = 8
Output
[[2,2,2,2],[2,3,3],[3,5]]
Constraints
1 <= candidates.length <= 30
2 <= candidates[i] <= 40
1 <= target <= 40

Topics

BacktrackingArraysRecursion

Implement combinationSum in the editor and return the answer — printing it is never graded.