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)) 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; +}; diff --git a/merge-two-sorted-lists/sukyoungshin.ts b/merge-two-sorted-lists/sukyoungshin.ts new file mode 100644 index 000000000..960f0bd9e --- /dev/null +++ b/merge-two-sorted-lists/sukyoungshin.ts @@ -0,0 +1,54 @@ +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; +}; + +// 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; + } +};