Key Idea
- Similar to '[Leetcode]46. Permutations' but allows the same start.
- [Leetcode]46. Permutations: hashnode.com/edit/clprlryz7000209jkd2ky246k
- Check if the sum of the previous path is equal to the target.
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
def dfs(start, path):
if sum(path) == target:
result.append(path.copy())
if sum(path) > target:
return
for i in range(start, len(candidates)):
path.append(candidates[i])
dfs(i, path)
path.pop()
result = []
dfs(0, [])
return result