Skip to content

Commit 578a50c

Browse files
committed
Combination Sum
1 parent 8132541 commit 578a50c

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

combination-sum/iam-edwin.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
6+
result = [[[] for _ in range(target + 1)] for _ in range(len(candidates))]
7+
8+
for index in range(len(candidates)):
9+
num = candidates[index]
10+
for subTarget in range(1, target + 1):
11+
maxUseCount = subTarget // num
12+
for useCount in range(0, maxUseCount + 1):
13+
subSubTarget = subTarget - num * useCount
14+
if subSubTarget == 0:
15+
result[index][subTarget].append([num] * useCount)
16+
else:
17+
for item in result[index - 1][subSubTarget]:
18+
result[index][subTarget].append(item + [num] * useCount)
19+
20+
return result[len(candidates) - 1][target]

0 commit comments

Comments
 (0)