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

[재호] WEEK 02 Solutions #339

Merged
merged 8 commits into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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,37 @@
// TC: O(N) - leetcode analyze 기준
Copy link
Contributor

Choose a reason for hiding this comment

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

복잡도 분석은 직접하시는 게 더 좋을 것 같습니다! 코딩 면접 때도 직접 하셔야하니까요 :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

넵, leetcode analyze complexity에 의존하지 않고 직접 계산해보겠습니다!

// SC: O(N)
Copy link
Contributor

Choose a reason for hiding this comment

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

혹시 배열 복재에 들어가는 메모리도 고려하셨을까요?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

평상시에 시간복잡도만 계산하고 공간복잡도는 대략적으로만 생각했는데 배열 복제의 메모리 사용도 고려해야하는지 몰랐습니다.. 😮
알고달레 블로그 확인해서 많이 도움 받았습니다 :) 감사합니다~!


/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {number[]} preorder
* @param {number[]} inorder
* @return {TreeNode}
*/
var buildTree = function (preorder, inorder) {
if (inorder.length === 0) {
return null;
}

if (inorder.length === 1) {
return new TreeNode(inorder[0]);
}
DaleSeo marked this conversation as resolved.
Show resolved Hide resolved

const rootValue = preorder[0];
const leftNodeLength = inorder.findIndex((value) => value === rootValue);
const leftNode = buildTree(
preorder.slice(1, 1 + leftNodeLength),
inorder.slice(0, leftNodeLength)
);
const rightNode = buildTree(
preorder.slice(1 + leftNodeLength),
inorder.slice(leftNodeLength + 1)
);
return new TreeNode(rootValue, leftNode, rightNode);
};
25 changes: 25 additions & 0 deletions counting-bits/wogha95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// TC: O(N)
// SC: O(N)

/**
* @param {number} n
* @return {number[]}
*/
var countBits = function (n) {
const result = [0];
let pointer = 0;
let lastPointer = 0;

for (let num = 1; num <= n; num++) {
result.push(result[pointer] + 1);

if (pointer === lastPointer) {
lastPointer = result.length - 1;
pointer = 0;
} else {
pointer += 1;
}
}

return result;
};
Comment on lines +8 to +25
Copy link
Contributor

Choose a reason for hiding this comment

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

이 투포인터 솔루션 신기한 것 같은데, 혹시 모임 때 소개해주실 수 있으실까요?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

달레님 알고리즘과 크게 다를 바 없다고 생각해서 조금 의아했는데 소개 이유를 들어보니 깨닫게 되었습니다!
그리고 갑작스럽게 발표준비하다보니 두서없이 설명드린것 같아서 걱정되었는데 부족한 부분 피드백 주셔서 감사합니다 :)

11 changes: 11 additions & 0 deletions valid-anagram/wogha95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// TC: O(N * log N)
// SC: O(N)

/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/
var isAnagram = function(s, t) {
return s.split('').sort().join('') === t.split('').sort().join('');
Copy link
Contributor

Choose a reason for hiding this comment

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

오..... 나누고 정렬하고 합치고 좋네요 배워갑니다!

};