forked from vJechsmayr/PythonAlgorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0077_Combinations.py
More file actions
38 lines (29 loc) · 1.24 KB
/
Copy path0077_Combinations.py
File metadata and controls
38 lines (29 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Solution:
def combine(self, n: int, k: int) -> List[List[int]]:
# Pretty much same as combination sums II, use index at most once per combo
# create list from 1 to n
# for each i-th element, call helper function from [i+1:]
# from the helper function, if length of arr passed in == k
# append and return
# otherwise, keep going for i+1 in a for loop in the helper
# ^ after done, delete the last index(backtrack process)
nums = [_ for _ in range(1,n+1)]
result = []
combo = []
n = len(nums)
for i in range(n):
combo.append(nums[i])
self.backtrack(nums[i+1:],combo, result, k)
combo = []
return result
def backtrack(self, nums: List[int], combo: List[int], result: List[List[int]], k: int) -> None:
if len(combo) == k:
result.append([_ for _ in combo])
return
n = len(nums)
for i in range(n):
combo.append(nums[i])
self.backtrack(nums[i+1:],combo,result,k)
# This is the backtrack process
del combo[-1]
return