Skip to content

Instantly share code, notes, and snippets.

@nma
Last active August 26, 2020 03:56
Show Gist options
  • Save nma/032c71ee6ccf68375cac8611aa1c105c to your computer and use it in GitHub Desktop.
Save nma/032c71ee6ccf68375cac8611aa1c105c to your computer and use it in GitHub Desktop.
Combinations Recursive
class Solution:
def combine(self, n: int, k: int) -> List[List[int]]:
nums = list(range(1, n + 1))
combinations = []
def combine_helper(ans: List[int], path: List[int], options: List[int], count: int):
if count == 0:
ans.append(path)
else:
for index, num in enumerate(options):
combine_helper(ans, path+[num], options[index + 1:], count - 1)
combine_helper(combinations, [], nums, k)
return combinations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment