From 4a8df0eb145b6320d95da39841fefff0663692f4 Mon Sep 17 00:00:00 2001 From: chae Date: Thu, 20 Feb 2025 14:30:15 +0900 Subject: [PATCH 1/5] feat: maximum-depth-of-binary-tree solution --- maximum-depth-of-binary-tree/YeomChaeeun.ts | 32 +++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 maximum-depth-of-binary-tree/YeomChaeeun.ts diff --git a/maximum-depth-of-binary-tree/YeomChaeeun.ts b/maximum-depth-of-binary-tree/YeomChaeeun.ts new file mode 100644 index 000000000..2387ee2bb --- /dev/null +++ b/maximum-depth-of-binary-tree/YeomChaeeun.ts @@ -0,0 +1,32 @@ +/** + * 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) + * - 공간 복잡도: O(n) + * @param root + */ +function maxDepth(root: TreeNode | null): number { + if(!root) return 0; + let max = 0; + let stack: [TreeNode | null, number][] = [[root, 1]]; + while(stack.length > 0) { + const [node, depth] = stack.pop(); + max = Math.max(depth, max); + if (node.left) stack.push([node.left, depth + 1]); + if (node.right) stack.push([node.right, depth + 1]); + } + return max +} \ No newline at end of file From 8b3ca33c1b587153459e0e5107d4863f40f698bb Mon Sep 17 00:00:00 2001 From: chae Date: Thu, 20 Feb 2025 14:33:15 +0900 Subject: [PATCH 2/5] =?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 --- maximum-depth-of-binary-tree/YeomChaeeun.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maximum-depth-of-binary-tree/YeomChaeeun.ts b/maximum-depth-of-binary-tree/YeomChaeeun.ts index 2387ee2bb..33740a6e4 100644 --- a/maximum-depth-of-binary-tree/YeomChaeeun.ts +++ b/maximum-depth-of-binary-tree/YeomChaeeun.ts @@ -29,4 +29,4 @@ function maxDepth(root: TreeNode | null): number { if (node.right) stack.push([node.right, depth + 1]); } return max -} \ No newline at end of file +} From c472fbc90285e3ee99ab3a05cbb7befba431e2c5 Mon Sep 17 00:00:00 2001 From: chae Date: Thu, 20 Feb 2025 16:30:20 +0900 Subject: [PATCH 3/5] feat: merge-intervals solution --- merge-intervals/YeomChaeeun.ts | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 merge-intervals/YeomChaeeun.ts diff --git a/merge-intervals/YeomChaeeun.ts b/merge-intervals/YeomChaeeun.ts new file mode 100644 index 000000000..cc6a924c4 --- /dev/null +++ b/merge-intervals/YeomChaeeun.ts @@ -0,0 +1,29 @@ +/** + * 겹치는 구간 합치기 + * 알고리즘 복잡도 + * - 시간 복잡도: O(n log n) - sort() 영향 + * - 공간 복잡도: O(n) + * @param intervals + */ +function merge(intervals: number[][]): number[][] { + if(intervals.length === 1) return intervals + + intervals.sort((a, b) => a[0] - b[0]); + + let results: number[][] = [] + let [tempX, tempY] = intervals[0] + + for(let i = 1; i < intervals.length; i++) { + let [x, y] = intervals[i] + if(x <= tempY) { + tempY = Math.max(tempY, y) + } else { + results.push([tempX, tempY]) + tempX = x + tempY = y + } + } + results.push([tempX, tempY]) + + return results; +} From bc2b7d143ec39af130e22dc372395dc0cdac679a Mon Sep 17 00:00:00 2001 From: chae Date: Fri, 21 Feb 2025 13:54:05 +0900 Subject: [PATCH 4/5] feat: reorder-list solution --- reorder-list/YeomChaeeun.ts | 51 +++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 reorder-list/YeomChaeeun.ts diff --git a/reorder-list/YeomChaeeun.ts b/reorder-list/YeomChaeeun.ts new file mode 100644 index 000000000..462120490 --- /dev/null +++ b/reorder-list/YeomChaeeun.ts @@ -0,0 +1,51 @@ +/** + * Definition for singly-linked list. + * class ListNode { + * val: number + * next: ListNode | null + * constructor(val?: number, next?: ListNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.next = (next===undefined ? null : next) + * } + * } + */ + +/** + Do not return anything, modify head in-place instead. + */ + +/** + * 리스트 재정렬 하기 (0 -> n -> 1 -> n-1 -> ...) + * 알고리즘 복잡도 + * - 시간 복잡도: O(n) + * - 공간 복잡도: O(n) + * @param head + */ +function reorderList(head: ListNode | null): void { + if (!head || !head.next) return; + + const stack: ListNode[] = []; + let node = head; + while (node) { + stack.push(node); + node = node.next; + } + + let left = 0; + let right = stack.length - 1; + + while (left < right) { + // 현재 노드의 다음에 마지막 노드 연결 + stack[left].next = stack[right]; + left++; + + // 남은 노드가 있으면 마지막 노드의 다음에 다음 왼쪽 노드 연결 + if (left < right) { + stack[right].next = stack[left]; + right--; + } + } + + // 마지막 노드의 next를 null로 설정 + stack[left].next = null; +} \ No newline at end of file From f3cf900d43e68704afb2592c8ebfedae44783f95 Mon Sep 17 00:00:00 2001 From: chae Date: Fri, 21 Feb 2025 13:56:23 +0900 Subject: [PATCH 5/5] =?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 --- reorder-list/YeomChaeeun.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reorder-list/YeomChaeeun.ts b/reorder-list/YeomChaeeun.ts index 462120490..da16a7b49 100644 --- a/reorder-list/YeomChaeeun.ts +++ b/reorder-list/YeomChaeeun.ts @@ -48,4 +48,4 @@ function reorderList(head: ListNode | null): void { // 마지막 노드의 next를 null로 설정 stack[left].next = null; -} \ No newline at end of file +}