less than 1 minute read

77. Combinations (medium)

class Solution:
    def combine(self, n: int, k: int) -> List[List[int]]:
        res = []
        def dfs(path, start):
            if len(path) == k:
                res.append(path[:])
            for i in range(start + 1, n + 1):
                path.append(i)
                dfs(path, i)
                path.pop()
        
        dfs([], 0)
        return res