-
-
Notifications
You must be signed in to change notification settings - Fork 361
[sonshn] WEEK 04 Solutions #2754
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
Merged
+77
−0
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
fcb31f4
contains-duplicate solution
sonshn 6c69e25
two-sum solution
sonshn 9d7e093
top-k-frequent-elements solution
sonshn 7766ac3
top-k-frequent-elements 풀이 추가
sonshn 32eb8b6
longest-consecutive-sequence solution
sonshn a1e4dde
house-robber solution
sonshn d460b73
valid-anagram solution
sonshn f3c4ef9
Merge branch 'DaleStudy:main' into main
sonshn 8d1ab21
climbing-stairs solution
sonshn 63da3f1
Merge branch 'main' of https://github.com/sonshn/leetcode-study
sonshn 1422ccd
valid-palindrome solution
sonshn aa73ee1
number of 1 bits solution
sonshn 7ed860a
number of 1 bits solution
sonshn 3ab760d
merge two sorted lists solution
sonshn 2fb1e56
maximum depth of binary tree solution
sonshn 2be8bc7
Merge branch 'DaleStudy:main' into main
sonshn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| /** | ||
| * 이진 트리의 최대 깊이를 재귀적으로 계산 (DFS) | ||
| * | ||
| * 시간 복잡도: O(n), n은 트리의 노드 수 | ||
| * 공간 복잡도: O(h), h는 트리의 높이, 재귀 호출로 인해 스택에 쌓이는 함수 호출의 깊이 때문 | ||
| */ | ||
| class Solution { | ||
| public int maxDepth(TreeNode root) { | ||
| if (root == null) { | ||
| return 0; | ||
| } | ||
|
|
||
| int leftDepth = maxDepth(root.left); | ||
| int rightDepth = maxDepth(root.right); | ||
|
|
||
| return Math.max(leftDepth, rightDepth) + 1; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * 이진 트리의 최대 깊이를 반복적으로 계산 (BFS) | ||
| * | ||
| * 시간 복잡도: O(n), n은 트리의 노드 수 | ||
| * 공간 복잡도: O(n), 큐에 저장되는 노드 수 때문 | ||
| */ | ||
| class Solution { | ||
| public int maxDepth(TreeNode root) { | ||
| if (root == null) { | ||
| return 0; | ||
| } | ||
|
|
||
| Queue<TreeNode> queue = new LinkedList<>(); | ||
| queue.offer(root); | ||
|
|
||
| int depth = 0; | ||
|
|
||
| while (!queue.isEmpty()) { | ||
| int size = queue.size(); | ||
|
|
||
| for (int i = 0; i < size; i++) { | ||
| TreeNode current = queue.poll(); | ||
|
|
||
| if (current.left != null) { | ||
| queue.offer(current.left); | ||
| } | ||
|
|
||
| if (current.right != null) { | ||
| queue.offer(current.right); | ||
| } | ||
| } | ||
|
|
||
| depth++; | ||
| } | ||
|
|
||
| return depth; | ||
| } | ||
| } |
|
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
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 재귀 호출 스택으로 인해 추가 공간이 필요하지만 각 재귀 호출마다 상위 노드 하나씩 차지합니다. 합병 자체는 선형 시간에 수행됩니다. 개선 제안: 현재 구현이 적절해 보입니다. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| /** | ||
| * 재귀를 사용하여 두 개의 정렬된 연결 리스트를 병합 | ||
| * | ||
| * 시간 복잡도: O(n + m), n은 list1의 길이, m은 list2의 길이 | ||
| * 공간 복잡도: O(n + m), 재귀 호출로 인해 스택에 쌓이는 함수 호출의 깊이 때문 | ||
| */ | ||
| class Solution { | ||
| public ListNode mergeTwoLists(ListNode list1, ListNode list2) { | ||
| if (list1 == null) return list2; | ||
| if (list2 == null) return list1; | ||
|
|
||
| if (list1.val <= list2.val) { | ||
| list1.next = mergeTwoLists(list1.next, list2); | ||
| return list1; | ||
| } else { | ||
| list2.next = mergeTwoLists(list1, list2.next); | ||
| return list2; | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
풀이 1:
Solution.maxDepth— Time: ✅ O(n) → O(n) / Space: ❌ O(h) → O(n)피드백: 트리 각 노드를 한 번씩 방문하므로 시간 복잡도는 노드 수에 선형적입니다. 재귀 호출 깊이는 트리의 최대 높이에 의해 공간이 결정됩니다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 2:
Solution.maxDepth— Time: ✅ O(n) → O(n) / Space: ✅ O(n) → O(n)피드백: 모든 노드를 한 번씩 방문하고 큐를 사용하여 레벨을 구분합니다. 최악의 경우 큐에 많은 노드가 남아 있을 수 있습니다.
개선 제안: 현재 구현이 적절해 보입니다.