diff --git a/find-minimum-in-rotated-sorted-array/seongmin36.js b/find-minimum-in-rotated-sorted-array/seongmin36.js new file mode 100644 index 0000000000..16682acbbd --- /dev/null +++ b/find-minimum-in-rotated-sorted-array/seongmin36.js @@ -0,0 +1,30 @@ +/** +이진탐색을 이용한 풀이 + +TC: O(log n) +SC: O(1) + */ +/** + * @param {number[]} nums + * @return {number} + */ +function findMin(nums) { + let left = 0; + let right = nums.length - 1; + + if (nums[left] <= nums[right]) return nums[left]; // 일자 배열인 경우 + + while (left < right) { + let mid = Math.floor((left + right) / 2); + + if (nums[mid] > nums[mid + 1]) return nums[mid + 1]; // mid의 바로 옆에서 초기화되는 경우 + + if (nums[mid] > nums[right]) { + left = mid + 1; + } else { + right = mid; + } + } + + return nums[left]; +} diff --git a/maximum-depth-of-binary-tree/seongmin36.js b/maximum-depth-of-binary-tree/seongmin36.js new file mode 100644 index 0000000000..021c7ef090 --- /dev/null +++ b/maximum-depth-of-binary-tree/seongmin36.js @@ -0,0 +1,25 @@ +/** +이 문제를 푸는 핵심은 DFS(깊이 우선 탐색)다. +왼쪽으로, 오른쪽으로 쭉 들어가는 값을 반환해서 변수에 저장한다. +이를 재귀호출하면 쉽게 해결할 수 있다. + */ +/** + * Definition for a binary tree node. + * function TreeNode(val, left, right) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + */ +/** + * @param {TreeNode} root + * @return {number} + */ +function maxDepth(root) { + if (root === null) return 0; + + let left_depth = maxDepth(root.left); + let right_depth = maxDepth(root.right); + + return Math.max(left_depth, right_depth) + 1; +} diff --git a/merge-two-sorted-lists/seongmin36.js b/merge-two-sorted-lists/seongmin36.js new file mode 100644 index 0000000000..22abc657ec --- /dev/null +++ b/merge-two-sorted-lists/seongmin36.js @@ -0,0 +1,38 @@ +/** +list1, list2는 시작 노드 객체 +핵심은 '가위바위보 기찻길 룰' +cur.next가 list1, list2 비교 결과를 배치해주는 역할 + +TC : O(n) +SC : O(1) + */ +/** + * Definition for singly-linked list. + * function ListNode(val, next) { + * this.val = (val===undefined ? 0 : val) + * this.next = (next===undefined ? null : next) + * } + */ +/** + * @param {ListNode} list1 + * @param {ListNode} list2 + * @return {ListNode} + */ +function mergeTwoLists(list1, list2) { + let dummy = new ListNode(); + let cur = dummy; + + while (list1 && list2) { + if (list1.val > list2.val) { + cur.next = list2; + list2 = list2.next; + } else { + cur.next = list1; + list1 = list1.next; + } + cur = cur.next; + } + cur.next = list1 || list2; // 남아있는 원소 붙이기 + + return dummy.next; +}