-
-
Notifications
You must be signed in to change notification settings - Fork 245
[Lyla] Week 14 #1095
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
Merged
[Lyla] Week 14 #1095
Changes from 4 commits
Commits
Show all changes
6 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,34 @@ | ||
''' | ||
시간 복잡도: O(n) | ||
공간 복잡도: O(n) | ||
''' | ||
|
||
from collections import deque | ||
from typing import List, Optional | ||
|
||
class TreeNode: | ||
def __init__(self, val=0, left=None, right=None): | ||
self.val = val | ||
self.left = left | ||
self.right = right | ||
|
||
class Solution: | ||
def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: | ||
queue = deque([root]) if root else [] | ||
result = [] | ||
|
||
while queue: | ||
same_level_nodes = [] | ||
|
||
for _ in range(len(queue)): | ||
node = queue.popleft() | ||
same_level_nodes.append(node.val) | ||
|
||
if node.left: | ||
queue.append(node.left) | ||
if node.right: | ||
queue.append(node.right) | ||
|
||
result.append(same_level_nodes) | ||
|
||
return result |
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,15 @@ | ||
''' | ||
시간 복잡도: O(n) | ||
공간 복잡도: O(n) | ||
''' | ||
from typing import List | ||
|
||
|
||
class Solution: | ||
def countBits(self, n: int) -> List[int]: | ||
dp = [0] * (n + 1) | ||
|
||
for i in range(1, n + 1): | ||
dp[i] = dp[i >> 1] + (i & 1) | ||
|
||
return 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,25 @@ | ||
''' | ||
시간 복잡도: O(n) | ||
공간 복잡도: O(n) | ||
''' | ||
from typing import List | ||
|
||
class Solution: | ||
def rob(self, nums: List[int]) -> int: | ||
n = len(nums) | ||
if n == 1: | ||
return nums[0] | ||
if n == 2: | ||
return max(nums[0], nums[1]) | ||
|
||
dp_first = [0] * n | ||
dp_second = [0] * n | ||
|
||
dp_first[0], dp_first[1] = nums[0], max(nums[0], nums[1]) | ||
dp_second[1], dp_second[2] = nums[1], max(nums[1], nums[2]) | ||
|
||
for i in range(2, n): | ||
dp_first[i] = max(dp_first[i - 1], dp_first[i - 2] + nums[i]) | ||
dp_second[i] = max(dp_second[i - 1], dp_second[i - 2] + nums[i]) | ||
|
||
return max(dp_first[-2], dp_second[-1]) |
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.
Queue 를 활용한 BFS 방식으로 문제를 풀어주셨네요!
DFS를 사용하여 풀어보시는것도 재미있을것 같습니다 :)