-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombination_sum_iii.py
42 lines (41 loc) · 1.6 KB
/
combination_sum_iii.py
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
39
40
41
42
class Solution(object):
def combinationSum3(self, k, n):
"""
:type k: int
:type n: int
:rtype: List[List[int]]
"""
# Use recursion to resolve the problem
# The algorithm complexity is high due to it has to iterate from one
# for each call given k, n. Another optimization is passing another
# parameter in mycombinationSum3(self, k, n, start) for tracking.
if k < 0 or k > 0 and ((10 - k) + 9)*k/2 < n:
return []
elif k == 1 and n < 10:
return [[n]]
# Check the worst recursion sitiation and try to avoid it.
elif (1 + k)*k/2 == n:
return [range(1, k + 1)]
# Check the worst recursion sitiation and try to avoid it.
elif ((10 - k) + 9)*k/2 == n:
return [range(9, 9 - k, -1)]
else:
l = []
for i in range(n):
if i > 0 and i <= n/2 and i < 10:
for j in self.combinationSum3(k - 1, n - i):
# If the number is not unique, then skip it.
# If the return list is empty, then skip it.
if i not in j and len(j) != 0:
j.append(i)
l.append(sorted(j))
# If the length of final list is less than 2, then return it.
if len(l) < 2:
return l
else:
# Drop any duplicated element.
c = []
for i in l:
if i not in c:
c.append(i);
return c