Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[highball] week2 solutions #371

Merged
merged 1 commit into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Comment on lines +9 to +10
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

 arr[i] = i % 2 === 0 ? arr[i / 2] : 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);
Copy link
Contributor

@tolluset tolluset Aug 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

arr.join('#') 도 가능하겠네용! 달레님 코멘트처럼 아스키문자를 피해야 될 것 같아여!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

문제에서 입력 문자열에 256개의 ASCII 문자가 들어있을 수 있다고 했는데요. #도 ASCII 문자여서 입력 문자열에 #에 포함되어 있는 경우 문제가 될 수 있을 것 같습니다.

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]++;
});
Comment on lines +5 to +13
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s와 t의 길이가 같지 않으면 anagram이 아니기 때문에 early retrun 하고 하나의 루프로 체크하는 것도 가능할 것 같아여!


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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sCharCount.every((count, i) => count === tCharCount[i]); 

로 확인할 수도 있겠네용!

검증이라면 every로 모든 값이 일치하는게 메소드가 조금 더 어울리는 것 같아여!

};