MCPcopy Create free account
hub / github.com/neetcode-gh/leetcode / combinationSum4

Method combinationSum4

python/0377-combination-sum-iv.py:2–24  ·  view source on GitHub ↗
(self, nums: List[int], target: int)

Source from the content-addressed store, hash-verified

1class 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

Calls 2

dfsFunction · 0.50
getMethod · 0.45

Tested by

no test coverage detected