MCPcopy Index your code
hub / github.com/neetcode-gh/leetcode / bfs

Function bfs

javascript/0046-permutations.js:40–60  ·  view source on GitHub ↗
(nums, levels = [[]], permutations = [])

Source from the content-addressed store, hash-verified

38};
39
40const bfs = (nums, levels = [[]], permutations = []) => {
41 for (const num of nums) {
42 for (let i = levels.length - 1; 0 <= i; i--) {
43 const previousLevel = levels.shift();
44
45 for (let index = 0; index < previousLevel.length + 1; index++) {
46 const level = reArrangeSet(previousLevel, num, index);
47
48 const isBaseCase = level.length === nums.length;
49 if (isBaseCase) {
50 permutations.push(level);
51 continue;
52 }
53
54 levels.push(level);
55 }
56 }
57 }
58
59 return permutations;
60};
61
62const reArrangeSet = (previousLevel, num, index) => {
63 const [before, after] = [

Callers 1

permuteFunction · 0.70

Calls 2

reArrangeSetFunction · 0.85
pushMethod · 0.45

Tested by

no test coverage detected