Skip to content

Commit 52f46f4

Browse files
authored
Merge pull request #362 from gitsunmin/main
[gitsunmin] WEEK 2 Solution
2 parents a85fe39 + d2f8a23 commit 52f46f4

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2+
/**
3+
* * 문제에서 정의된 타입
4+
*/
5+
export class TreeNode {
6+
val: number
7+
left: TreeNode | null
8+
right: TreeNode | null
9+
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
10+
this.val = (val === undefined ? 0 : val)
11+
this.left = (left === undefined ? null : left)
12+
this.right = (right === undefined ? null : right)
13+
}
14+
}
15+
16+
/**
17+
* ! 문제에서의 Output과 실제 정의된, 사용되는 output이 다르기 때문에, 한 번 변환 작업을 거처야함. (실제 제출 시 제외한 함수 입니다.)
18+
*/
19+
// function treeToArray(root: TreeNode | null): (number | null)[] {
20+
// if (!root) return [];
21+
// const result: (number | null)[] = [];
22+
// const queue: (TreeNode | null)[] = [root];
23+
// while (queue.length > 0) {
24+
// const node = queue.shift();
25+
// if (node) {
26+
// result.push(node.val);
27+
// queue.push(node.left);
28+
// queue.push(node.right);
29+
// } else {
30+
// result.push(null);
31+
// }
32+
// }
33+
// while (result[result.length - 1] === null) result.pop();
34+
// return result;
35+
// }
36+
37+
function buildTree(preorder: number[], inorder: number[]): TreeNode | null {
38+
if (preorder.length === 0 || inorder.length === 0) return null;
39+
40+
const rootVal = preorder[0];
41+
const inorderIndex = inorder.indexOf(rootVal);
42+
const leftInorder = inorder.slice(0, inorderIndex);
43+
44+
return new TreeNode(
45+
rootVal,
46+
buildTree(
47+
preorder.slice(1, 1 + leftInorder.length),
48+
leftInorder
49+
),
50+
buildTree(
51+
preorder.slice(1 + leftInorder.length),
52+
inorder.slice(inorderIndex + 1)
53+
),
54+
);
55+
}
56+
57+
58+
// const preorder = [3, 9, 20, 15, 7];
59+
// const inorder = [9, 3, 15, 20, 7];
60+
// console.log('output:', treeToArray(buildTree(preorder, inorder)));

counting-bits/gitsunmin.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* https://leetcode.com/problems/counting-bits/
3+
* time complexity : O(n)
4+
* space complexity : O(n)
5+
*/
6+
function countBits(n: number): number[] {
7+
const arr = new Array(n + 1).fill(0);
8+
for (let i = 1; i <= n; i++) arr[i] = arr[i >> 1] + (i & 1);
9+
return arr;
10+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* https://www.lintcode.com/problem/659/
3+
* time complexity : O(n)
4+
* space complexity : O(n)
5+
* ! emoji는 입력에서 제외한다.
6+
*/
7+
8+
function encode(input: Array<string>): string {
9+
return input.join("😀");
10+
}
11+
12+
function decode(input: string): Array<string> {
13+
return input.split("😀");
14+
}

valid-anagram/gitsunmin.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* https://leetcode.com/problems/valid-anagram/submissions
3+
* time complexity : O(n)
4+
* space complexity : O(n)
5+
*/
6+
function isAnagram(s: string, t: string): boolean {
7+
if (s.length !== t.length) return false;
8+
9+
const map = {};
10+
11+
for (const char of s) map[char] = (map[char] ?? 0) + 1;
12+
13+
for (const char of t) {
14+
if (map[char] !== undefined) {
15+
map[char] = map[char] - 1;
16+
} else return false;
17+
}
18+
19+
for (const val of Object.values(map)) if (val !== 0) return false;
20+
21+
return true;
22+
};

0 commit comments

Comments
 (0)