Skip to content

[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

Merged
merged 1 commit into from
Mar 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions kth-smallest-element-in-a-bst/Chaedie.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

공간 복잡도를 O(h) h는 재귀 스택의 높이, 로 풀이하려면 어떤 방법을 사용할 수 있을까요?

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]
46 changes: 46 additions & 0 deletions lowest-common-ancestor-of-a-binary-search-tree/Chaedie.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

최악의 경우 모든 노드를 방문해야 해서 제 생각에는
시간 복잡도 : O(n) n은 노드의 갯수
공간 복잡도 : O(h) h는 재귀 스택의 높이
라고 생각되는데 어떠실까요?

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
18 changes: 18 additions & 0 deletions meeting-rooms/Chaedie.py
Copy link
Contributor

Choose a reason for hiding this comment

The 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