Skip to content

Commit

Permalink
Merge pull request #371 from highballplz/main
Browse files Browse the repository at this point in the history
[highball] week2 solutions
  • Loading branch information
highballplz authored Aug 25, 2024
2 parents 45ea72e + 3b9560e commit 2712a2a
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
function TreeNode(val, left, right) {
this.val = val === undefined ? 0 : val;
this.left = left === undefined ? null : left;
this.right = right === undefined ? null : right;
}

const buildTree = function (preorder, inorder) {
if (preorder.length === 0) return null;

const rootNode = new TreeNode(preorder[0]);
const rootValueIndexAtInorder = inorder.indexOf(preorder[0]);

const leftLength = rootValueIndexAtInorder - 0;
const rightLength = inorder.length - 1 - rootValueIndexAtInorder;

const leftPreorder = preorder.slice(1, 1 + leftLength);
const leftInorder = inorder.slice(0, 0 + leftLength);
rootNode.left = buildTree(leftPreorder, leftInorder);

const rightPreorder = preorder.slice(
1 + leftLength,
1 + leftLength + rightLength
);
const rightInorder = inorder.slice(
rootValueIndexAtInorder + 1,
rootValueIndexAtInorder + 1 + rightLength
);
rootNode.right = buildTree(rightPreorder, rightInorder);

return rootNode;
};

function bfsTraversal(root) {
const treeValues = [];

const queue = [];
queue.push(root);

while (queue.length > 0) {
const n = queue.length;

for (let i = 0; i < n; i++) {
const currentNode = queue.shift();

if (currentNode) {
treeValues.push(currentNode.val);
queue.push(currentNode.left);
queue.push(currentNode.right);
} else {
treeValues.push(null);
queue.push(null);
queue.push(null);
}
}

if (queue.every((el) => el === null)) break;
}

return treeValues;
}

// console.log(bfsTraversal(buildTree([3, 9, 20, 15, 7], [9, 3, 15, 20, 7])));
console.log(bfsTraversal(buildTree([1, 2], [2, 1])));
14 changes: 14 additions & 0 deletions counting-bits/highball.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const countBits = function (n) {
const arr = new Array(n + 1);
arr[0] = 0;
if (n === 0) return arr;
arr[1] = 1;
if (n === 1) return arr;

for (i = 2; i <= n; i++) {
if (i % 2 === 0) arr[i] = arr[i / 2];
else arr[i] = arr[(i - 1) / 2] + 1;
}

return arr;
};
9 changes: 9 additions & 0 deletions encode-and-decode-strings/highball.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const encode = function (arr) {
const encodedStr = arr.reduce((acc, cur) => acc + "#" + cur);
return encodedStr;
};

const decode = function (str) {
const decodedArr = str.split("#");
return decodedArr;
};
16 changes: 16 additions & 0 deletions valid-anagram/highball.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const isAnagram = function (s, t) {
const sCharCount = new Array(26).fill(0);
const tCharCount = new Array(26).fill(0);

Array.from(s).forEach((char) => {
const charAsInt = char.charCodeAt(0) - 97;
sCharCount[charAsInt]++;
});

Array.from(t).forEach((char) => {
const charAsInt = char.charCodeAt(0) - 97;
tCharCount[charAsInt]++;
});

return sCharCount.reduce((acc, cur, i) => acc && cur === tCharCount[i], true);
};

0 comments on commit 2712a2a

Please sign in to comment.