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
26 changes: 26 additions & 0 deletions insert-interval/Seoya0512.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'''
Time Complexity : O(N)
- newInterval을 삽입할 위치 탐색 O(N)
- Intervals 병합을 위해 for문으로 모든 구간을 순회 O(N)
- O(N) + O(N) = O(N)

Space Complexity : O(N)
- output 리스트에 모든 구간을 저장 O(N)

'''
class Solution:
def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
idx = 0
while idx < len(intervals) and intervals[idx][0] < newInterval[0]:
idx +=1
intervals.insert(idx, newInterval) # newInterval을 삽입할 위치 탐색 (O(N))
Copy link
Contributor

Choose a reason for hiding this comment

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

저는 분기를 이용해 앞/겹침/뒤 케이스를 처리했는데, 삽입 위치를 index 로 처리하신 점이 흥미로웠습니다.


# Intervals 병합
output = [intervals[0]]
for interval in intervals[1:]:
# 이전 구간과 겹치는 경우
if output[-1][1] >= interval[0]:
output[-1][1] = max(output[-1][1], interval[1])
else:
output.append(interval)
return output
29 changes: 29 additions & 0 deletions kth-smallest-element-in-a-bst/Seoya0512.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'''
Time Complexity: O(N)
- dfs 함수가 모든 노드를 중위순회 방식으로 방문함므로 O(N) 소요

Space Complexity: O(N)
- aligned_arr 리스트에 모든 노드의 값을 저장하므로 O(N) 소요
- 재귀 호출 스택이 최대 트리의 높이만큼 쌓일 수 있으므로 최악의 경우 O(N) 소요
'''

# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def kthSmallest(self, root: Optional[TreeNode], k: int) -> int:
aligned_arr = []

def dfs(node):
if not node:
return
dfs(node.left)
aligned_arr.append(node.val)
dfs(node.right)

dfs(root)

return aligned_arr[k - 1]
12 changes: 12 additions & 0 deletions lowest-common-ancestor-of-a-binary-search-tree/Seoya0512.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'''
Time Complexity: O(H)
- H는 트리의 높이
'''

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)
if p.val > root.val and q.val > root.val:
return self.lowestCommonAncestor(root.right, p, q)
return root
19 changes: 19 additions & 0 deletions meeting-rooms/Seoya0512.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'''
Time Complexity: O(N log N)
- intervals.sort() 는 O(N log N) 소요
- for loop 는 O(N) 소요
- 전체적으로 O(N log N) + O(N) = O(N log N)

Space Complexity: O(1)
- end_time, start_time 상수 변수 추가로 사용
'''
def can_attend_meetings(self, intervals: List[Interval]) -> bool:
intervals.sort()
end_time = intervals[0][1]

for i in range(1, len(intervals)):
start_time = intervals[i][0]
if end_time > start_time:
return False
end_time = intervals[i][1]
return True