diff --git a/insert-interval/Seoya0512.py b/insert-interval/Seoya0512.py new file mode 100644 index 0000000000..113447e329 --- /dev/null +++ b/insert-interval/Seoya0512.py @@ -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)) + + # 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 diff --git a/kth-smallest-element-in-a-bst/Seoya0512.py b/kth-smallest-element-in-a-bst/Seoya0512.py new file mode 100644 index 0000000000..c90af8b43d --- /dev/null +++ b/kth-smallest-element-in-a-bst/Seoya0512.py @@ -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] diff --git a/lowest-common-ancestor-of-a-binary-search-tree/Seoya0512.py b/lowest-common-ancestor-of-a-binary-search-tree/Seoya0512.py new file mode 100644 index 0000000000..de5bb29c1f --- /dev/null +++ b/lowest-common-ancestor-of-a-binary-search-tree/Seoya0512.py @@ -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 diff --git a/meeting-rooms/Seoya0512.py b/meeting-rooms/Seoya0512.py new file mode 100644 index 0000000000..e3b469ff24 --- /dev/null +++ b/meeting-rooms/Seoya0512.py @@ -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