Combination Sum

Solution
class Solution:
    def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
        ans = []
        
        def backtrack(i, cur, total):
            if total == target:
                ans.append(list(cur))
                return
            if i >= len(candidates) or total > target:
                return
            
            cur.append(candidates[i])
            backtrack(i, cur, total + candidates[i])
            cur.pop()
            backtrack(i + 1, cur, total)
            
        backtrack(0, [], 0)
        return ans

Time Complexity

O(2^T)

Space Complexity

O(T)