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

Function dfs

javascript/0040-combination-sum-ii.js:14–39  ·  view source on GitHub ↗
(
    candidates,
    target,
    index = 0,
    combination = [],
    combinations = [],
)

Source from the content-addressed store, hash-verified

12};
13
14const dfs = (
15 candidates,
16 target,
17 index = 0,
18 combination = [],
19 combinations = [],
20) => {
21 const isBaseCase = target < 0;
22 if (isBaseCase) return combinations;
23
24 const isTarget = target === 0;
25 if (isTarget) {
26 if (combination.length) combinations.push(combination.slice());
27
28 return combinations;
29 }
30
31 for (let i = index; i < candidates.length; i++) {
32 const isDuplicate = index < i && candidates[i - 1] === candidates[i];
33 if (isDuplicate) continue;
34
35 backTrack(candidates, target, i, combination, combinations);
36 }
37
38 return combinations;
39};
40
41const backTrack = (candidates, target, i, combination, combinations) => {
42 combination.push(candidates[i]);

Callers 2

combinationSum2Function · 0.70
backTrackFunction · 0.70

Calls 2

backTrackFunction · 0.70
pushMethod · 0.45

Tested by

no test coverage detected