-
-
Notifications
You must be signed in to change notification settings - Fork 334
[liza0525] WEEK 14 Solutions #2629
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
Open
liza0525
wants to merge
2
commits into
DaleStudy:main
Choose a base branch
from
liza0525:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+72
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,56 @@ | ||
| # 7기 풀이 | ||
| class Solution: | ||
|
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. BFS와 DFS 두 가지 방식으로 레벨 순회를 모두 잘 구현해 주셔서, 트리 탐색 패턴을 비교하면서 학습하기 좋은 코드였어요 👍 |
||
| # 풀이 1 - BFS | ||
| # 시간 복잡도: O(n) | ||
| # - 노드의 개수(n)만큼 모두 탐색하므로 | ||
| # 공간 복잡도: O(w) | ||
| # - 최악은 한 레벨의 최대 노드 수(w)만큼 queue에 쌓임 | ||
| def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: | ||
| res = [] | ||
|
|
||
| nodes = deque() | ||
| if root: | ||
| nodes.appendleft(root) | ||
|
|
||
| while nodes: | ||
| childs = deque() | ||
| sibling_vals = [] | ||
| while nodes: | ||
| node = nodes.pop() | ||
| if not node: | ||
| continue | ||
|
|
||
| sibling_vals.append(node.val) | ||
| if node.left: | ||
| childs.appendleft(node.left) | ||
| if node.right: | ||
| childs.appendleft(node.right) | ||
|
|
||
| res.append(sibling_vals) | ||
| nodes = childs | ||
|
|
||
| return res | ||
|
|
||
| # 풀이 2 - DFS | ||
| # 시간 복잡도: O(n) | ||
| # - 노드의 개수(n)만큼 모두 탐색하므로 | ||
| # 공간 복잡도: O(h) | ||
| # - 재귀 스택이 최대 나무의 높이(h)만큼 쌓임 | ||
| def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: | ||
| res = [] | ||
|
|
||
| def dfs(node, depth): | ||
| if not node: | ||
| return | ||
|
|
||
| if len(res) == depth: | ||
| res.append([node.val]) | ||
| else: | ||
| res[depth].append(node.val) | ||
|
|
||
| dfs(node.left, depth + 1) | ||
| dfs(node.right, depth + 1) | ||
|
|
||
| dfs(root, 0) | ||
|
|
||
| return res | ||
|
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: DP 배열을 이용하여 이전 결과를 활용하며, 모든 수에 대해 한 번씩 계산합니다. 개선 제안: 현재 구현이 적절해 보입니다. |
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,16 @@ | ||
| # 7기 풀이 | ||
| # 시간 복잡도: O(n) | ||
| # - n의 크기만큼 모든 수에 대한 dp 값을 찾음 | ||
| # 공간 복잡도: O(n) | ||
| # - n의 크기만큼 dp 어레이를 사용 | ||
| class Solution: | ||
| def countBits(self, n: int) -> List[int]: | ||
| dp = [0 for _ in range(n + 1)] | ||
|
|
||
| for i in range(1, n + 1): | ||
| if i % 2 == 1: | ||
| dp[i] = dp[i - 1] + 1 | ||
| else: | ||
| dp[i] = dp[i // 2] | ||
|
|
||
| return dp |
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.levelOrder— Time: ✅ O(n) → O(n) / Space: ✅ O(w) → O(w)피드백: 큐를 이용한 BFS 탐색으로 모든 노드를 한 번씩 방문하며, 최대 노드 수(w)에 비례하는 공간을 사용합니다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 2:
Solution.levelOrder— Time: ✅ O(n) → O(n) / Space: ✅ O(h) → O(h)피드백: 재귀 호출 스택이 트리의 높이(h)에 비례하며, 모든 노드를 한 번씩 방문합니다.
개선 제안: 현재 구현이 적절해 보입니다.