From c6d37afa758e87b0f3f2fada15bb21e051f01aeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EB=AF=BC?= Date: Mon, 19 Aug 2024 00:53:38 +0900 Subject: [PATCH 1/5] Add week 2 solutions: valid-anagram --- valid-anagram/gitsunmin.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 valid-anagram/gitsunmin.ts 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; +}; From 71307c3eaf1e1adda2e4fcbba329a2788ba05a61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EB=AF=BC?= Date: Mon, 19 Aug 2024 23:56:17 +0900 Subject: [PATCH 2/5] Add week 2 solutions: counting-bits --- counting-bits/gitsunmin.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 counting-bits/gitsunmin.ts 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; +} From 24e7e835f9574b99fa2b5b85ffd137ebad59ef53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EB=AF=BC?= Date: Tue, 20 Aug 2024 18:57:40 +0900 Subject: [PATCH 3/5] Add week 2 solutions: encode-and-decode-strings --- encode-and-decode-strings/gitsunmin.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 encode-and-decode-strings/gitsunmin.ts 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("😀"); +} From 56eafe1447de168a15cd47766e616ae816c6959d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EB=AF=BC?= Date: Wed, 21 Aug 2024 23:09:36 +0900 Subject: [PATCH 4/5] Add week 2 solutions: construct-binary-tree-from-preorder-and-inorder-traversal --- .../gitsunmin.ts | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 construct-binary-tree-from-preorder-and-inorder-traversal/gitsunmin.ts 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..cf8124ba4 --- /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))); \ No newline at end of file From ad8c107022ecfb3cdcbd17bcbed0ea6fa5aa47b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EB=AF=BC?= Date: Sat, 24 Aug 2024 01:19:17 +0900 Subject: [PATCH 5/5] =?UTF-8?q?=F0=9F=9A=A8=20add=20line=20break?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gitsunmin.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/construct-binary-tree-from-preorder-and-inorder-traversal/gitsunmin.ts b/construct-binary-tree-from-preorder-and-inorder-traversal/gitsunmin.ts index cf8124ba4..8b3e1b02d 100644 --- a/construct-binary-tree-from-preorder-and-inorder-traversal/gitsunmin.ts +++ b/construct-binary-tree-from-preorder-and-inorder-traversal/gitsunmin.ts @@ -57,4 +57,4 @@ function buildTree(preorder: number[], inorder: number[]): TreeNode | null { // const preorder = [3, 9, 20, 15, 7]; // const inorder = [9, 3, 15, 20, 7]; -// console.log('output:', treeToArray(buildTree(preorder, inorder))); \ No newline at end of file +// console.log('output:', treeToArray(buildTree(preorder, inorder)));