Combination Sum
01 · Problem
Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may return the combinations in any order.
The same number may be chosen from candidates an unlimited number of times. Two combinations are unique if the frequency of at least one of the chosen numbers is different.
The test cases are generated such that the number of unique combinations that sum up to target is less than 150 combinations for the given input.
02 · Examples
candidates = [2,3,6,7], target = 7
[[2,2,3],[7]]
2 + 2 + 3 = 7 and 7 = 7 are the only two combinations that sum to the target.
candidates = [2,3,5], target = 8
[[2,2,2,2],[2,3,3],[3,5]]
All three unique combinations that sum to 8 using the given candidates.
candidates = [2], target = 1
[]
No combination of 2s can produce a sum of 1.
03 · Constraints
- 011 <= candidates.length <= 30
- 022 <= candidates[i] <= 40
- 03All elements of candidates are distinct
- 041 <= target <= 40
04 · Optimal complexity
- Time
- O(n^(T/M))
- Space
- O(T/M)
Practice it alone or rehearse it as an interview.
Practice Mode gives you an editor and test runs, nothing else. AI Interview Mode puts a voice interviewer on the other side, adds a clock, and ends with a scored summary of the round.