-
-
Notifications
You must be signed in to change notification settings - Fork 361
[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
base: main
Are you sure you want to change the base?
Changes from all commits
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,27 @@ | ||
| /** | ||
| 이 문제를 푸는 핵심은 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); | ||
|
|
||
| let count = Math.max(left_depth, right_depth) + 1; | ||
|
|
||
| return count; | ||
| } |
|
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 배열 및 sort를 사용하므로 추가 공간과 정렬 비용이 발생한다. 리스트를 직접 합치며 정렬하는 방법으로 개선 가능. 개선 제안: 고려해볼 만한 대안: 두 리스트를 한 번 순회하며 병합 정렬 방식으로 O(n) 시간과 O(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) 시간복잡도로도 해결하실수 있으세요! 한번 시도해보시는것도 좋겠네요 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| /** | ||
| ListNode는 파라미터로 current value와 next value를 지닌다. | ||
| 배열 메서드도 사용할 수 없다. | ||
|
|
||
| 나는 원초적인 방법으로 해결해보았다. | ||
| result 배열에 모든 수를 다 넣고, sort()하는 것이다. | ||
| 여기서 중요한 점은 '어떻게 배열 → ListNode로 변경하느냐'이다. | ||
| [1, 2, 3] 배열을 1 → 2 → 3 리스트 노드를 만들기 위해서는 컴퓨터 구조상 역방향인 3부터 가져와야한다. | ||
| 이때 사용한 메서드는 'reduceRight()'다. | ||
| reduceRight() 메서드는 reduce() 작업순서의 역방향이다. | ||
|
|
||
| TC : O(nLogN) → sort() | ||
| SC : O(n) | ||
| */ | ||
| /** | ||
| * 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 result = []; | ||
| let node1 = list1; | ||
| let node2 = list2; | ||
|
|
||
| while (node1 !== null) { | ||
| result.push(node1.val); | ||
| node1 = node1.next; | ||
| } | ||
|
|
||
| while (node2 !== null) { | ||
| result.push(node2.val); | ||
| node2 = node2.next; | ||
| } | ||
|
|
||
| return result | ||
| .sort((a, b) => a - b) | ||
| .reduceRight((next, val) => new ListNode(val, next), null); | ||
| } |
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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 트리의 모든 노드를 한 번씩 방문하여 깊이를 계산한다. 재귀 스택의 최대 깊이가 트리의 높이가 된다.
개선 제안: 현재 구현이 적절해 보입니다.