Skip to content
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
22 changes: 22 additions & 0 deletions lowest-common-ancestor-of-a-binary-search-tree/aa601.py
Copy link
Contributor

Choose a reason for hiding this comment

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

TC: O(n) 최악의 경우 모든 노드를 방문
SC: O(h) 재귀 스택의 높이 만큼의 공간
이 되지 않을까요? :)

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'''
TC: O(h) (h는 트리의 높이)
SC: O(1)
풀이 방법: 이진 탐색 트리의 성질을 이용해서
p,q의 값이 현재 노드 사이에 있거나 p,q의 값 중 하나라도 현재 노드와 같을 때까지 탐색한다
'''
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

class Solution:
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
while root:
if p.val < root.val and q.val < root.val:
root = root.left
elif p.val > root.val and q.val > root.val:
root = root.right
else:
return root
12 changes: 12 additions & 0 deletions maximum-depth-of-binary-tree/aa601.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'''
TC: O(n)
SC: O(n)
'''

class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
# 재귀함수 호출 시 노드가 존재하지 않을 때 탈출한다
if not root:
return 0
# root를 기준으로 왼쪽 서브트리, 오른쪽 서브트리를 재귀호출로 쌓아놓고 리프노드부터 세면서 트리의 깊이를 구한다
return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1
32 changes: 32 additions & 0 deletions meeting-rooms/aa601.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'''
TC: O(nlogn)
SC: O(1)
풀이 방법: 구간 중 첫번째 인자에 대해 정렬한 뒤, 이웃한 인자에 대해서만 겹치는지 확인한다
시간 복잡도는 정렬에 드는 nlogn만큼 필요하다
'''

from typing import (
List,
)
from lintcode import (
Interval,
)
"""
Definition of Interval:
class Interval(object):
def __init__(self, start, end):
self.start = start
self.end = end
"""

class Solution:
"""
@param intervals: an array of meeting time intervals
@return: if a person could attend all meetings
"""
def can_attend_meetings(self, intervals: List[Interval]) -> bool:
intervals.sort(key=lambda x: x.start)
for i in range(1, len(intervals)):
if intervals[i - 1].end > intervals[i].start:
return False
return True