📘
Combination Sum
MediumBacktracking
Given an array of distinct integers candidates and a target integer target, return all unique combinations where the chosen numbers sum to target. The same number may be chosen unlimited times. Output each combination sorted, one per line, combinations sorted lexicographically.
Example:
Input:
2,3,6,7 7
Output:
2 2 3 7
Reasoning:
- The problem starts by checking all possible combinations of the given numbers 2,3,6,7 that sum to the target 7.
- It finds two valid combinations: 2+2+3=7 and 7=7, where the same number can be chosen unlimited times.
- These combinations are then sorted and output one per line, with the combinations themselves sorted lexicographically: 223 and 7.
- The final output is a list of these combinations, each on a new line, resulting in: 2 2 3 7
Constraints:
- 1 <= len(candidates) <= 30
- 2 <= candidates[i] <= 40
- 1 <= target <= 40
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.