From 2830df0d4cda132f53982c87b25f7d169c67bbcd Mon Sep 17 00:00:00 2001 From: SuKyoung Date: Mon, 21 Apr 2025 11:29:08 +0900 Subject: [PATCH 1/4] add: #224 Merge Two Sorted Lists --- merge-two-sorted-lists/sukyoungshin.ts | 39 ++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 merge-two-sorted-lists/sukyoungshin.ts diff --git a/merge-two-sorted-lists/sukyoungshin.ts b/merge-two-sorted-lists/sukyoungshin.ts new file mode 100644 index 000000000..0a7fda3ca --- /dev/null +++ b/merge-two-sorted-lists/sukyoungshin.ts @@ -0,0 +1,39 @@ +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; + } +} + +function mergeTwoLists( + list1: ListNode | null, + list2: ListNode | null +): ListNode | null { + const dummy = new ListNode(); + let current = dummy; + + const addNode = (val: number) => { + current.next = new ListNode(val); + current = current.next; + }; + + while (list1 !== null && list2 !== null) { + if (list1.val < list2.val) { + addNode(list1.val); + list1 = list1.next; + } else if (list1.val > list2.val) { + addNode(list2.val); + list2 = list2.next; + } else { + addNode(list1.val); + addNode(list2.val); + list1 = list1.next; + list2 = list2.next; + } + } + + current.next = list1 !== null ? list1 : list2; + return dummy.next; +}; From 85c5b267811595b8f3153f738edb3cd4a4f83b3c Mon Sep 17 00:00:00 2001 From: SuKyoung Date: Mon, 21 Apr 2025 13:27:10 +0900 Subject: [PATCH 2/4] =?UTF-8?q?add:=20#224=20Merge=20Two=20Sorted=20Lists?= =?UTF-8?q?=20=EB=91=90=EB=B2=88=EC=A7=B8=20=ED=92=80=EC=9D=B4=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- merge-two-sorted-lists/sukyoungshin.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/merge-two-sorted-lists/sukyoungshin.ts b/merge-two-sorted-lists/sukyoungshin.ts index 0a7fda3ca..960f0bd9e 100644 --- a/merge-two-sorted-lists/sukyoungshin.ts +++ b/merge-two-sorted-lists/sukyoungshin.ts @@ -37,3 +37,18 @@ function mergeTwoLists( current.next = list1 !== null ? list1 : list2; return dummy.next; }; + +// 2번째 풀이 (재귀) +function mergeTwoLists2( + list1: ListNode | null, + list2: ListNode | null +): ListNode | null { + if (!(list1 && list2)) return list1 || list2; + if (list1.val < list2.val) { + list1.next = mergeTwoLists(list1.next, list2); + return list1; + } else { + list2.next = mergeTwoLists(list1, list2.next); + return list2; + } +}; From 8ac93baf4c91c001f748d341784860717ca85feb Mon Sep 17 00:00:00 2001 From: SuKyoung Date: Thu, 24 Apr 2025 14:10:11 +0900 Subject: [PATCH 3/4] add: #227 Maximum Depth of Binary Tree --- maximum-depth-of-binary-tree/sukyoungshin.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 maximum-depth-of-binary-tree/sukyoungshin.ts diff --git a/maximum-depth-of-binary-tree/sukyoungshin.ts b/maximum-depth-of-binary-tree/sukyoungshin.ts new file mode 100644 index 000000000..bdf0a0853 --- /dev/null +++ b/maximum-depth-of-binary-tree/sukyoungshin.ts @@ -0,0 +1,19 @@ +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; + } +} + +function maxDepth(root: TreeNode | null): number { + if (!root) return 0; + + const leftDepth = maxDepth(root.left); + const rightDepth = maxDepth(root.right); + + return Math.max(leftDepth, rightDepth) + 1; +}; From 59449bde867a331c052cbe12c69c1ee1c9896b33 Mon Sep 17 00:00:00 2001 From: SuKyoung Date: Fri, 25 Apr 2025 14:52:57 +0900 Subject: [PATCH 4/4] add: #245 Find Minimum In Rotated Sorted Array --- find-minimum-in-rotated-sorted-array/sukyoungshin.ts | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 find-minimum-in-rotated-sorted-array/sukyoungshin.ts diff --git a/find-minimum-in-rotated-sorted-array/sukyoungshin.ts b/find-minimum-in-rotated-sorted-array/sukyoungshin.ts new file mode 100644 index 000000000..c88487fc1 --- /dev/null +++ b/find-minimum-in-rotated-sorted-array/sukyoungshin.ts @@ -0,0 +1,6 @@ +// 1. Brute-force (시간복잡도: O(n)) +function findMin(nums: number[]): number { + return Math.min(...nums); +}; + +// 2 Binary Search (시간복잡도: O(log n))