Skip to content

Commit ecc6ecc

Browse files
committed
[:solved] #254
1 parent 3673158 commit ecc6ecc

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

combination-sum/ppxyn1.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# idea: brute force
2+
# Complexity: O(len(candidates)^(target / min(candidates)))
3+
4+
class Solution:
5+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
6+
candidates.sort()
7+
result = []
8+
nums = []
9+
10+
def dfs(start: int, total: int):
11+
if total > target:
12+
return
13+
if total == target:
14+
result.append(nums.copy())
15+
return
16+
for i in range(start, len(candidates)):
17+
num = candidates[i]
18+
if total + num > target:
19+
break
20+
nums.append(num)
21+
dfs(i, total + num)
22+
nums.pop()
23+
dfs(0, 0)
24+
return result
25+
26+
27+

0 commit comments

Comments
 (0)