(
nums,
target,
total,
index = 0,
sum = 0,
memo = initMemo(nums, total),
)
| 62 | ); /* Time O(M) | Space O(M) */ |
| 63 | |
| 64 | const calculate = ( |
| 65 | nums, |
| 66 | target, |
| 67 | total, |
| 68 | index = 0, |
| 69 | sum = 0, |
| 70 | memo = initMemo(nums, total), |
| 71 | ) => { |
| 72 | const isBaseCase = index === nums.length; |
| 73 | if (isBaseCase) { |
| 74 | const isTarget = sum === target; |
| 75 | if (isTarget) return 1; |
| 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | const hasSeen = memo[index][sum + total] != null; |
| 81 | if (hasSeen) return memo[index][sum + total]; |
| 82 | |
| 83 | return dfs( |
| 84 | nums, |
| 85 | target, |
| 86 | total, |
| 87 | index, |
| 88 | sum, |
| 89 | memo, |
| 90 | ); /* Time O(N * M) | Space O((N * M) + HEIGHT) */ |
| 91 | }; |
| 92 | |
| 93 | var dfs = (nums, target, total, index, sum, memo) => { |
| 94 | const left = calculate( |
no test coverage detected