-
-
Notifications
You must be signed in to change notification settings - Fork 362
[seongmin36] WEEK 04 Solutions #2747
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1ab1e6f
e938765
64e4ddc
869ca89
642fbb1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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]) { | ||
|
Comment on lines
+17
to
+22
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 탐색 과정에서 정답으로 판정할 수 있는 경우가 있어서 한 번 최적화 시도해보셔도 좋을 거 같습니다.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 그렇네요, 회전된 정렬 배열이라는 점에서 최솟값을 바로 찾을 수 있겠네요! 피드백 감사합니다 |
||
| left = mid + 1; | ||
| } else { | ||
| right = mid; | ||
| } | ||
| } | ||
|
|
||
| return nums[left]; | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 리프까지 모든 노드를 한 번씩 방문하며 재귀로 깊이를 계산한다. 최악의 경우 트리의 높이가 공간 복잡도에 영향 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| } |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. O(1) 공간복잡도와 O(N) 시간복잡도로도 해결하실수 있으세요! 한번 시도해보시는것도 좋겠네요
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 가상 머리(dummy) 노드를 사용해 비교하며 결과를 구성한다. 남은 노드를 연결하는 부분까지 일관되게 처리 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 중간값 비교를 통해 회전점 인근으로 범위를 점진 축소한다. 공통적으로 좌우 중 어느 쪽이 더 큰지에 따라 탐색 구간을 반으로 줄인다.
개선 제안: 현재 구현이 적절해 보입니다.