-
-
Notifications
You must be signed in to change notification settings - Fork 305
[ppxyn1] WEEK 14 solutions #2324
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
ppxyn1
wants to merge
1
commit into
DaleStudy:main
Choose a base branch
from
ppxyn1: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.
+43
−0
Open
Changes from all commits
Commits
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,30 @@ | ||
| # Definition for a binary tree node. | ||
| # class TreeNode: | ||
| # def __init__(self, val=0, left=None, right=None): | ||
| # self.val = val | ||
| # self.left = left | ||
| # self.right = right | ||
|
|
||
|
|
||
| # idea: BFS | ||
| # Time Complexity: O(n) | ||
| from collections import deque | ||
| class Solution: | ||
| def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: | ||
| ans = [] | ||
| q = deque([root]) | ||
| while q: | ||
| level = [] | ||
| for i in range(len(q)): | ||
| node = q.popleft() | ||
| if node: | ||
| level.append(node.val) | ||
| if node.left: | ||
| q.append(node.left) | ||
| if node.right: | ||
| q.append(node.right) | ||
| if level: | ||
| ans.append(level) | ||
| return ans | ||
|
|
||
|
|
|
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,13 @@ | ||
| # idea : - | ||
| # Time Complexity : O(n log n) since counting? | ||
| class Solution: | ||
| def countBits(self, n: int) -> List[int]: | ||
| ans = [] | ||
| for i in range(n + 1): | ||
| ans.append(bin(i)[2:].count("1")) | ||
| return ans | ||
|
|
||
| # TODO: O(n)? | ||
|
|
||
|
|
||
|
|
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.
level을 따로 측정하셨군요! 같은 레벨의 노드는 while 내부의 for문이 도는 과정에서 모두 pop되니, 좋은 방법이라고 생각됩니다.