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

Function backtrack

javascript/0473-matchsticks-to-square.js:28–45  ·  view source on GitHub ↗
(i)

Source from the content-addressed store, hash-verified

26 matchsticks = matchsticks.sort((a, b) => b - a);
27
28 function backtrack(i) {
29 if (ans) return;
30 if (i >= matchsticks.length) {
31 if (check(sides)) {
32 ans = true;
33 }
34 return;
35 }
36 for (let j = 0; j < 4; j++) {
37 if (sides[j] + matchsticks[i] > max_size) {
38 continue;
39 }
40 sides[j] += matchsticks[i];
41
42 backtrack(i + 1);
43 sides[j] -= matchsticks[i];
44 }
45 }
46 backtrack(0);
47
48 return ans;

Callers 1

makesquareFunction · 0.70

Calls 1

checkFunction · 0.70

Tested by

no test coverage detected