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

Function calculate

javascript/0494-target-sum.js:64–91  ·  view source on GitHub ↗
(
    nums,
    target,
    total,
    index = 0,
    sum = 0,
    memo = initMemo(nums, total),
)

Source from the content-addressed store, hash-verified

62 ); /* Time O(M) | Space O(M) */
63
64const 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
93var dfs = (nums, target, total, index, sum, memo) => {
94 const left = calculate(

Callers 2

findTargetSumWaysFunction · 0.85
dfsFunction · 0.85

Calls 2

initMemoFunction · 0.70
dfsFunction · 0.70

Tested by

no test coverage detected