Combinations
Solution Code
class Solution:
def combine(self, n: int, k: int) -> List[List[int]]:
res = []
def backtrack(start, path):
if len(path) == k:
res.append(path[:])
return
for i in range(start, n + 1):
path.append(i)
backtrack(i + 1, path)
path.pop()
backtrack(1, [])
return resTime Complexity
O(k C(n, k)
Space Complexity
O(k)

