Method
combinationSum4
(self, nums: List[int], target: int)
Source from the content-addressed store, hash-verified
| 1 | class Solution: |
| 2 | def combinationSum4(self, nums: List[int], target: int) -> int: |
| 3 | cache = {0: 1} |
| 4 | |
| 5 | for total in range(1, target + 1): |
| 6 | cache[total] = 0 |
| 7 | for n in nums: |
| 8 | cache[total] += cache.get(total - n, 0) |
| 9 | return cache[target] |
| 10 | |
| 11 | def dfs(total): |
| 12 | if total == target: |
| 13 | return 1 |
| 14 | if total > target: |
| 15 | return 0 |
| 16 | if total in cache: |
| 17 | return cache[total] |
| 18 | |
| 19 | cache[total] = 0 |
| 20 | for n in nums: |
| 21 | cache[total] += dfs(total + n) |
| 22 | return cache[total] |
| 23 | |
| 24 | return dfs(0) |
Callers
nothing calls this directly
Tested by
no test coverage detected