-
-
Notifications
You must be signed in to change notification settings - Fork 195
[thispath98] Week 11 #1041
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
[thispath98] Week 11 #1041
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9ccd081
feat: Add Reorder List solution
thispath98 959c10c
feat: Maximum Depth of Binary Tree solution
thispath98 2e059f5
feat: add Merge Intervals solutions
thispath98 269516b
feat: fix Space Complexity for Maximum Depth of Binary Tree solution
thispath98 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,27 @@ | ||
class Solution: | ||
def maxDepth(self, root: Optional[TreeNode]) -> int: | ||
""" | ||
Intuition: | ||
왼쪽 자식과 오른쪽 자식에 대해 depth를 증가시키며 재귀하고, | ||
둘 중 큰 값을 반환한다. | ||
|
||
Time Complexity: | ||
O(N): | ||
모든 노드에 대해 재귀적으로 호출하므로 O(N)이다. | ||
|
||
Space Complexity: | ||
O(h): | ||
트리의 높이 h만큼 재귀 함수를 호출하므로, | ||
공간 복잡도는 O(h)이다. | ||
""" | ||
|
||
def get_depth(node, depth): | ||
if not node: | ||
return depth | ||
|
||
left = get_depth(node.left, depth + 1) | ||
right = get_depth(node.right, depth + 1) | ||
return max(left, right) | ||
|
||
answer = get_depth(root, 0) | ||
return answer |
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,28 @@ | ||
class Solution: | ||
def merge(self, intervals: List[List[int]]) -> List[List[int]]: | ||
""" | ||
Intuition: | ||
정렬된 intervals을 순회하면서 이전의 인터벌과 현재 인터벌이 | ||
겹친다면, 이를 갱신해준다. | ||
겹치지 않는다면, 새로운 인터벌을 추가한다. | ||
|
||
Time Complexity: | ||
O(N log N): | ||
최초에 정렬을 하므로, O(N log N)이다. | ||
이후 한번 스캔을 하므로 O(N)이다. | ||
따라서 시간 복잡도는 O(N log N)이다. | ||
|
||
Space Complexity: | ||
O(N): | ||
answer에 N개의 값을 저장하므로 O(N)이다. | ||
""" | ||
sorted_intervals = sorted(intervals) | ||
|
||
answer = [sorted_intervals[0]] | ||
for start, end in sorted_intervals[1:]: | ||
prev_start, prev_end = answer[-1] | ||
if prev_end >= start: | ||
answer[-1][1] = max(prev_end, end) | ||
else: | ||
answer.append([start, end]) | ||
return answer |
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. nodes 배열에 모든 노드를 담아 O(n) 으로 풀이해 주셨네요! |
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,31 @@ | ||
class Solution: | ||
def reorderList(self, head: Optional[ListNode]) -> None: | ||
""" | ||
Intuition: | ||
nodes 리스트에 모든 노드를 저장한다. | ||
현재 노드가 -i - 1번째 노드일 때, | ||
현재 노드의 next는 head의 next로 설정하고, | ||
head의 next는 현재 노드로 설정한다. | ||
이후 마지막에 중간에 있는 노드를 None으로 설정한다. | ||
|
||
Time Complexity: | ||
O(N): | ||
모든 노드를 순회하므로 O(N)이다. | ||
|
||
Space Complexity: | ||
O(N): | ||
모든 노드를 nodes 리스트에 저장하므로 O(N)이다. | ||
""" | ||
nodes = [] | ||
node = head | ||
while node: | ||
nodes.append(node) | ||
node = node.next | ||
|
||
for i in range((len(nodes) - 1) // 2): | ||
cur = nodes[-i - 1] | ||
cur.next = head.next | ||
head.next = cur | ||
head = cur.next | ||
|
||
nodes[len(nodes) // 2].next = None |
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.
공간 복잡도를 answer 변수 사용으로 O(1) 으로 잡아주셨는데, 해당 문제 풀이를 DFS 를 사용하셔서 제 생각에는 DFS의 호출에 따라 그만큼 스택이 쌓여서 Tree의 높이인 h만큼이 소요되여 O(h)가 될 것 같습니다. 한번 더 확인 해주시면 감사하겠습니다!
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.
재귀 호출 스택도 공간 복잡도에 포함시켜야 하는군요. 감사합니다!