forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0377.py
31 lines (26 loc) · 755 Bytes
/
0377.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
class Solution:
def combinationSum4(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
mem = dict()
nums.sort()
return self._combinationSum4(nums, target, mem)
def _combinationSum4(self, nums, target, mem):
if target == 0:
return 1
if target in mem:
return mem[target]
result = 0
for num in nums:
if num > target:
break
result += self._combinationSum4(nums, target - num, mem)
mem[target] = result
return result
if __name__ == '__main__':
nums = [1, 2, 4]
target = 32
print(Solution().combinationSum4(nums, target))