Combination Sum LeetCode solution in Python
1# Runtime: 95 ms (Top 82.54%) | Memory: 14.1 MB (Top 72.96%)2class Solution:3def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:45path = []6answer = []78def dp(idx, total):9if total == target:10answer.append(path[:])11return12if total > target:13return1415for i in range(idx, len(candidates)):16path.append(candidates[i])17dp(i, total + candidates[i])18path.pop()1920dp(0, 0)21return answer