# 39. Combination sum

Source: leetcode

https://leetcode-cn.com/problems/combination-sum

## subject

Given an array of candidates without duplicate elements and a target number target, find out all combinations in candidates that can make the sum of numbers target.

The number in candidates can be selected repeatedly without restriction.

**explain:**

- All numbers, including target, are positive integers.
- The solution set cannot contain repeated combinations.

**Example 1:**

```
Input: candidates = [2,3,6,7], target = 7,
The solution set is as follows:
[
[7],
[2,2,3]
]
```

**Example 2:**

```
Input: candidates = [2,3,5], target = 8,
The solution set is as follows:
[
[2,2,2,2],
[2,3,3],
[3,5]
]
```

**Tips:**

- 1 <= candidates.length <= 30
- 1 <= candidates[i] <= 200
- Each element in the candidate is unique.
- 1 <= target <= 500

## Thinking of problem solving

### Idea: backtracking

First examine the question. The title is given an array of non repeating elements and the target value. It is required to find out all combinations in the array that can make the sum of array elements target.

The elements in the array are all positive integers, so the elements in the array can be reused, but there can be no duplicate combination in the solution set.

Here we can see example 1:

```
Input: candidates = [2,3,6,7], target = 7,
The solution set is as follows:
[
[7],
[2,2,3]
]
```

The answer here is not [2,3,2] or [3,2,2], because these two are regarded as the combination of repetition with [2,2,3], which needs special attention here.

The following is a simple illustration:

Here’s $/ color {red}{ A kind of }$means that this element is not selected, so that the repeated combination of elements can be avoided{ A kind of }$indicates that the current path selection element and is greater than the target value target, while $\ color {green}{ A kind of }$represents the current path selection element and is equal to the target value, which can be added to the return list.

In the above illustration, only one branch is simply drawn, but the strategy for selecting other branches is the same, which is omitted here. If there is something you don’t quite understand, it is suggested that you can complete it on the draft and help yourself understand it.

Then, in accordance with the ideas in the above legend, the code is implemented, as follows.

```
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
#Results list
ans = []
#Possible combinations
tmp = []
def helper(idx, total):
"Trace back to find the sum of combinations
Args:
IDX: select element index
Total: elements and in composition
"""
#Base conditions
#When the sum of the elements is greater than the target value, it is returned directly
if total > target:
return
#When the sum of the elements is equal to the target value, the combination is added to the result and returned
if total == target:
ans.append(tmp[::])
return
#Enter branches and avoid repeated combinations
for i in range(idx, len(candidates)):
#Update the total value,
total += candidates[i]
#Also attempts to add the current element to the composition
tmp.append(candidates[i])
#Recursion again
#Here you can see the article legend, recursion down, the optional elements are selected from themselves
#At the same time, composition duplication can be avoided because the corresponding elements in front of index I will not be selected again
helper(i, total)
#Backtracking, backtracking, combining elements and total values
tmp.pop()
total -= candidates[i]
total = 0
helper(0, total)
return ans
```

## Welcome to our attention

The official account [book collection]