Skip to content

[gitsunmin] WEEK 2 Solution #362

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

Merged
merged 7 commits into from
Aug 24, 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,60 @@

/**
* * 문제에서 정의된 타입
*/
export class TreeNode {
val: number
left: TreeNode | null
right: TreeNode | null
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
this.val = (val === undefined ? 0 : val)
this.left = (left === undefined ? null : left)
this.right = (right === undefined ? null : right)
}
}

/**
* ! 문제에서의 Output과 실제 정의된, 사용되는 output이 다르기 때문에, 한 번 변환 작업을 거처야함. (실제 제출 시 제외한 함수 입니다.)
*/
// function treeToArray(root: TreeNode | null): (number | null)[] {
// if (!root) return [];
// const result: (number | null)[] = [];
// const queue: (TreeNode | null)[] = [root];
// while (queue.length > 0) {
// const node = queue.shift();
// if (node) {
// result.push(node.val);
// queue.push(node.left);
// queue.push(node.right);
// } else {
// result.push(null);
// }
// }
// while (result[result.length - 1] === null) result.pop();
// return result;
// }

function buildTree(preorder: number[], inorder: number[]): TreeNode | null {
if (preorder.length === 0 || inorder.length === 0) return null;

const rootVal = preorder[0];
const inorderIndex = inorder.indexOf(rootVal);
const leftInorder = inorder.slice(0, inorderIndex);

return new TreeNode(
rootVal,
buildTree(
preorder.slice(1, 1 + leftInorder.length),
leftInorder
),
buildTree(
preorder.slice(1 + leftInorder.length),
inorder.slice(inorderIndex + 1)
),
);
}


// const preorder = [3, 9, 20, 15, 7];
// const inorder = [9, 3, 15, 20, 7];
// console.log('output:', treeToArray(buildTree(preorder, inorder)));
10 changes: 10 additions & 0 deletions counting-bits/gitsunmin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* https://leetcode.com/problems/counting-bits/
* time complexity : O(n)
* space complexity : O(n)
*/
function countBits(n: number): number[] {
const arr = new Array(n + 1).fill(0);
for (let i = 1; i <= n; i++) arr[i] = arr[i >> 1] + (i & 1);
return arr;
}
14 changes: 14 additions & 0 deletions encode-and-decode-strings/gitsunmin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* https://www.lintcode.com/problem/659/
* time complexity : O(n)
* space complexity : O(n)
* ! emoji는 입력에서 제외한다.
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

@gitsunmin gitsunmin Aug 24, 2024

Choose a reason for hiding this comment

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

문제에 다른 제약이 없어서 특수문자 대신에 이모지를 넣어봤어요 ㅎ

*/

function encode(input: Array<string>): string {
return input.join("😀");
}

function decode(input: string): Array<string> {
return input.split("😀");
}
22 changes: 22 additions & 0 deletions valid-anagram/gitsunmin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* https://leetcode.com/problems/valid-anagram/submissions
* time complexity : O(n)
* space complexity : O(n)
*/
function isAnagram(s: string, t: string): boolean {
if (s.length !== t.length) return false;

const map = {};

for (const char of s) map[char] = (map[char] ?? 0) + 1;

for (const char of t) {
if (map[char] !== undefined) {
map[char] = map[char] - 1;
} else return false;
}

for (const val of Object.values(map)) if (val !== 0) return false;

return true;
};