Combinations

PHOTO EMBED

Thu Mar 10 2022 02:19:49 GMT+0000 (Coordinated Universal Time)

Saved by @vijuhiremath #python #combinations #template

def combine(self, n: int, k: int) -> List[List[int]]:
        res = []
        nums = range(1,n+1);

        def dfs(k, index, path):
            if k == 0:
                res.append(path)
                return # backtracking 
            for i in range(index, len(nums)):
                dfs(k-1, i+1, path+[nums[i]])
               
        dfs(k, 0 ,[])
        return res
content_copyCOPY

https://leetcode.com/problems/combination-sum/discuss/429538/General-Backtracking-questions-solutions-in-Python-for-reference-%3A