-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path216-Combination-Sum-III.py
More file actions
34 lines (26 loc) · 866 Bytes
/
216-Combination-Sum-III.py
File metadata and controls
34 lines (26 loc) · 866 Bytes
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
'''Leetcode- https://leetcode.com/problems/combination-sum-iii/'''
'''
Find all valid combinations of k numbers that sum up to n such that the following conditions are true:
Only numbers 1 through 9 are used.
Each number is used at most once.
Return a list of all possible valid combinations. The list must not contain the same combination twice, and the combinations may be returned in any order.
Example 1:
Input: k = 3, n = 7
Output: [[1,2,4]]
Explanation:
1 + 2 + 4 = 7
There are no other valid combinations.
'''
def combinationSum3(k, n):
res = []
def backtrack(cur, n, start):
if k == len(cur):
if n == 0:
res.append(cur.copy())
return
for i in range(start, 10):
cur.append(i)
backtrack(cur, n-i, i+1)
cur.pop()
backtrack([], n, 1)
return res