Combination Sum
Given an array of distinct integers candidates and a target integer, return all unique combinations of candidates where the chosen numbers sum to target. The same number may be chosen an unlimited number of times. Two combinations are unique if the multiset of chosen numbers differs.
Open official problem prompt ↗Enumerate every multiset of candidates (repeats allowed) whose sum equals the target, with no combination listed twice.
Making exact change for a price using coins of unlimited supply: you can use several of the same coin, but [nickel, dime] and [dime, nickel] are the same handful of coins.
- Input
- candidates = [2, 3, 6, 7], target = 7
- Output
- [[2,2,3], [7]]
- Why
- 2+2+3 = 7 and 7 = 7 are the only ways to reach 7; e.g. 2+2+2 = 6 falls short and adding another 2 overshoots.
1 <= candidates.length <= 302 <= candidates[i] <= 40All elements of candidates are distinct1 <= target <= 40