-
-
Notifications
You must be signed in to change notification settings - Fork 195
[Chaedie] Week 13 #1078
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
[Chaedie] Week 13 #1078
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
""" | ||
Solution: | ||
1) preOrder Traversal | ||
2) k 번쨰 요소 return | ||
|
||
Time: O(n) | ||
Space: O(n) | ||
|
||
""" | ||
|
||
|
||
class Solution: | ||
def kthSmallest(self, root: Optional[TreeNode], k: int) -> int: | ||
arr = [] | ||
|
||
def dfs(node): | ||
if not node: | ||
return | ||
|
||
dfs(node.left) | ||
arr.append(node.val) | ||
dfs(node.right) | ||
|
||
dfs(root) | ||
|
||
return arr[k - 1] |
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. 최악의 경우 모든 노드를 방문해야 해서 제 생각에는 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
""" | ||
Solution: | ||
BST 의 특징을 이용해 풀이할 예정이다. | ||
1) cur.val 보다 p.val, q.val 이 작으면 왼쪽 트리에 LCA 가 있다. | ||
2) cur.val 보다 p.val, q.val 이 크면 오른쪽 트리에 LCA 가 있다. | ||
3) 나머지 케이스는 본인이 LCA 이다. | ||
|
||
Time: O(log(n)) | ||
Space: O(1) | ||
|
||
""" | ||
|
||
|
||
class Solution: | ||
def lowestCommonAncestor( | ||
self, root: "TreeNode", p: "TreeNode", q: "TreeNode" | ||
) -> "TreeNode": | ||
|
||
cur = root | ||
while cur: | ||
if p.val < cur.val and q.val < cur.val: | ||
cur = cur.left | ||
elif cur.val < p.val and cur.val < q.val: | ||
cur = cur.right | ||
else: | ||
return cur | ||
|
||
|
||
""" | ||
Solution: 재귀 | ||
Time: O(log(n)) | ||
Space: O(log(n)) | ||
""" | ||
|
||
|
||
class Solution: | ||
def lowestCommonAncestor( | ||
self, root: "TreeNode", p: "TreeNode", q: "TreeNode" | ||
) -> "TreeNode": | ||
|
||
if p.val < root.val and q.val < root.val: | ||
return self.lowestCommonAncestor(root.left, p, q) | ||
elif p.val > root.val and q.val > root.val: | ||
return self.lowestCommonAncestor(root.right, p, q) | ||
else: | ||
return root |
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. 정렬 알고리즘의 시간 복잡도는 O(n log n) 이라고 생각되는데 python에서 sort() 사용시 시간 복잡도가 궁금하네요! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
""" | ||
Solution: | ||
1) 정렬 | ||
2) prev endTime 이 cur startTime 보다 큰 경우가 있으면 return False | ||
Time: O(n) | ||
Space: O(1) | ||
""" | ||
|
||
|
||
class Solution: | ||
def canAttendMeetings(self, intervals: List[List[int]]) -> bool: | ||
intervals.sort() | ||
|
||
for i in range(1, len(intervals)): | ||
if intervals[i - 1][1] > intervals[i][0]: | ||
return False | ||
|
||
return True |
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.
공간 복잡도를 O(h) h는 재귀 스택의 높이, 로 풀이하려면 어떤 방법을 사용할 수 있을까요?