From 91742b0c3f1b55d21778c01e5a1d898f1307b5f6 Mon Sep 17 00:00:00 2001 From: chae Date: Mon, 16 Dec 2024 18:01:41 +0900 Subject: [PATCH 1/6] feat: valid-anagram solution --- valid-anagram/YeomChaeEun.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 valid-anagram/YeomChaeEun.ts diff --git a/valid-anagram/YeomChaeEun.ts b/valid-anagram/YeomChaeEun.ts new file mode 100644 index 000000000..c235d4def --- /dev/null +++ b/valid-anagram/YeomChaeEun.ts @@ -0,0 +1,17 @@ +function isAnagram(s: string, t: string): boolean { + if(s.length !== t.length) return false + + let counter = {} + for(let sValue of s) { + if(!counter[sValue]) { counter[sValue] = 1 } + else { counter[sValue]++ } + + } + + for(let tValue of t) { + if(!counter[tValue]) return false + else counter[tValue]-- + } + + return Object.values(counter).findIndex(value => value !== 0) < 0; +}; \ No newline at end of file From b44fbbeeb1ff63645c430b4c1ca87ec7e7cd5064 Mon Sep 17 00:00:00 2001 From: chae Date: Mon, 16 Dec 2024 18:08:06 +0900 Subject: [PATCH 2/6] =?UTF-8?q?fix:=20=EA=B0=9C=ED=96=89=20=EB=AC=B8?= =?UTF-8?q?=EC=9E=90=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- valid-anagram/YeomChaeEun.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/valid-anagram/YeomChaeEun.ts b/valid-anagram/YeomChaeEun.ts index c235d4def..fabde7be8 100644 --- a/valid-anagram/YeomChaeEun.ts +++ b/valid-anagram/YeomChaeEun.ts @@ -14,4 +14,4 @@ function isAnagram(s: string, t: string): boolean { } return Object.values(counter).findIndex(value => value !== 0) < 0; -}; \ No newline at end of file +} From 7dc8c64efd32902716836ac4868931f277e4c781 Mon Sep 17 00:00:00 2001 From: YeomChaeeun Date: Sat, 21 Dec 2024 12:15:59 +0900 Subject: [PATCH 3/6] feat: climbing-stairs solution --- climbing-stairs/YeomChaeeun.ts | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 climbing-stairs/YeomChaeeun.ts diff --git a/climbing-stairs/YeomChaeeun.ts b/climbing-stairs/YeomChaeeun.ts new file mode 100644 index 000000000..910213584 --- /dev/null +++ b/climbing-stairs/YeomChaeeun.ts @@ -0,0 +1,26 @@ +/** + * 계단 오르기 + * 알고리즘 복잡도: + * - 시간복잡도: O(n) - 입력값 크기에 비례하는 단일 반복문 + * - 공간복잡도: O(1) - 상수 개의 변수만 사용 + * @param n + */ +function climbStairs(n: number): number { + // 1 - 2 - 3 - 5 - 8 ... 규칙 발생 + if(n <= 3) return n + + // 접근 (1) - 시간복잡도가 너무 큼 + // return climbStairs(n - 1) + climbStairs(n - 2) // 시간 + + // 접근 (2) + // 피보나치 수열과 비슷한 앞의 두 숫자를 더해서 배열 구조를 만듬 + let current = 1; // 현재 방법 + let prev = 1; // 이전 단계의 방법 + + // n-1번 반복하여 계산 + for (let i = 1; i < n; i++) { + [current, prev] = [current + prev, current]; + } + + return current; +} From eddea6e13ffa41926b6e4922a42369400a450322 Mon Sep 17 00:00:00 2001 From: YeomChaeeun Date: Sat, 21 Dec 2024 17:16:30 +0900 Subject: [PATCH 4/6] feat: 3sum solution --- 3sum/YeomChaeeun.ts | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 3sum/YeomChaeeun.ts diff --git a/3sum/YeomChaeeun.ts b/3sum/YeomChaeeun.ts new file mode 100644 index 000000000..54732ce96 --- /dev/null +++ b/3sum/YeomChaeeun.ts @@ -0,0 +1,42 @@ +/** + * 세 숫자의 합이 0이 되는 조합 찾기 + * 알고리즘 복잡도: + * - 시간복잡도: O(n^2) + * - 공간복잡도: O(1) + * @param nums + */ +function threeSum(nums: number[]): number[][] { + // 정렬 + nums.sort((a, b) => a - b) + let result: number[][] = [] + + // 투포인터 + for (let i = 0; i < nums.length - 2; i++) { + if (i > 0 && nums[i] === nums[i - 1]) continue; + + let start = i + 1 + let end = nums.length - 1 + const target = -nums[i] // 고정 숫자를 이용 + // start + end + target === 0 이므로, start + end === -target + + while (start < end) { + const sum = nums[start] + nums[end] + if (sum === target) { + result.push([nums[i], nums[start], nums[end]]) + + // 배열 중복 값 건너뛰기 + while (start < end && nums[start] === nums[start + 1]) start++ + while (start < end && nums[start] === nums[end - 1]) end-- + + // 포인터 이동 + start++ + end-- + } else if (sum < target) { + start++ + } else { + end-- + } + } + } + return result +} From 7f870fa424b26e99a923384402cdebf2d01239ea Mon Sep 17 00:00:00 2001 From: YeomChaeeun Date: Sat, 21 Dec 2024 18:32:36 +0900 Subject: [PATCH 5/6] feat: construct-binary-tree-from-preorder-and-inorder-traversal solution --- .../YeomChaeeun.ts | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 construct-binary-tree-from-preorder-and-inorder-traversal/YeomChaeeun.ts diff --git a/construct-binary-tree-from-preorder-and-inorder-traversal/YeomChaeeun.ts b/construct-binary-tree-from-preorder-and-inorder-traversal/YeomChaeeun.ts new file mode 100644 index 000000000..c51beafdc --- /dev/null +++ b/construct-binary-tree-from-preorder-and-inorder-traversal/YeomChaeeun.ts @@ -0,0 +1,42 @@ +/** + * Definition for a binary tree node. + * 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) + * } + * } + */ +/** + * 이진트리 만들기 + * 알고리즘 복잡도: + * - 시간복잡도: O(n^2) + * - 공간복잡도: O(n^2) + */ +function buildTree(preorder: number[], inorder: number[]): TreeNode | null { + // 전위 순회(preorder): 최상위 노드 -> 좌측 서브트리 -> 우측 서브트리 + // 중위 순회(inorder): 좌측 서브트리 -> 최상위 노드 -> 우측 서브트리 + + // 재귀적으로 호출하기 위해 배열이 비었을때 null을 반환하며 종료시킴 + if (preorder.length === 0 || inorder.length === 0) { + return null + } + + let root = preorder[0] + let mid = inorder.findIndex((value) => value === root) + + let leftInorder = inorder.slice(0, mid) + let rightInorder = inorder.slice(mid+1) + + let leftPreorder = preorder.slice(1, 1 + leftInorder.length) + let rightPreorder = preorder.slice(1 + leftInorder.length) + + let left = buildTree(leftPreorder, leftInorder) + let right = buildTree(rightPreorder, rightInorder) + + return new TreeNode(root, left, right) +} From 0ae2dba8b2666230ceb4a78e10efe1e9211d5b62 Mon Sep 17 00:00:00 2001 From: YeomChaeeun Date: Sat, 21 Dec 2024 18:37:39 +0900 Subject: [PATCH 6/6] =?UTF-8?q?feat:=20valid-anagram=20solution=20?= =?UTF-8?q?=EB=B3=B5=EC=9E=A1=EB=8F=84=20=EB=B6=84=EC=84=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- valid-anagram/YeomChaeEun.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/valid-anagram/YeomChaeEun.ts b/valid-anagram/YeomChaeEun.ts index fabde7be8..974dedc31 100644 --- a/valid-anagram/YeomChaeEun.ts +++ b/valid-anagram/YeomChaeEun.ts @@ -1,3 +1,10 @@ +/** + * 알고리즘 복잡도: + * - 시간복잡도: O(n) + * - 공간복잡도: O(k) + * @param s + * @param t + */ function isAnagram(s: string, t: string): boolean { if(s.length !== t.length) return false