diff --git a/construct-binary-tree-from-preorder-and-inorder-traversal/gitsunmin.ts b/construct-binary-tree-from-preorder-and-inorder-traversal/gitsunmin.ts new file mode 100644 index 000000000..8b3e1b02d --- /dev/null +++ b/construct-binary-tree-from-preorder-and-inorder-traversal/gitsunmin.ts @@ -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))); diff --git a/counting-bits/gitsunmin.ts b/counting-bits/gitsunmin.ts new file mode 100644 index 000000000..b2865cc50 --- /dev/null +++ b/counting-bits/gitsunmin.ts @@ -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; +} diff --git a/encode-and-decode-strings/gitsunmin.ts b/encode-and-decode-strings/gitsunmin.ts new file mode 100644 index 000000000..927eb5419 --- /dev/null +++ b/encode-and-decode-strings/gitsunmin.ts @@ -0,0 +1,14 @@ +/** + * https://www.lintcode.com/problem/659/ + * time complexity : O(n) + * space complexity : O(n) + * ! emoji는 입력에서 제외한다. + */ + +function encode(input: Array): string { + return input.join("😀"); +} + +function decode(input: string): Array { + return input.split("😀"); +} diff --git a/valid-anagram/gitsunmin.ts b/valid-anagram/gitsunmin.ts new file mode 100644 index 000000000..010b229f7 --- /dev/null +++ b/valid-anagram/gitsunmin.ts @@ -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; +};