diff --git a/find-median-from-data-stream/alphaorderly.py b/find-median-from-data-stream/alphaorderly.py new file mode 100644 index 0000000000..9cd7fba199 --- /dev/null +++ b/find-median-from-data-stream/alphaorderly.py @@ -0,0 +1,27 @@ +""" +시간 복잡도: O(LogN) +공간 복잡도: O(N) + +최소 힙에는 스트림의 중간값보다 크거나 같은 값들이 저장됨 +최대 힙에는 스트림의 중간값보다 작은 값들이 저장됨 + +두 힙의 경계에 위치한 값을 이용해 중간값을 구한다 +""" +class MedianFinder: + + def __init__(self): + self.min = [] + self.max = [] + + def addNum(self, num: int) -> None: + heapq.heappush_max(self.max, num) + heapq.heappush(self.min, heapq.heappop_max(self.max)) + + if len(self.min) > len(self.max): + heapq.heappush_max(self.max, heapq.heappop(self.min)) + + def findMedian(self) -> float: + if len(self.max) == len(self.min): + return (self.min[0] + self.max[0]) / 2 + else: + return self.max[0] diff --git a/insert-interval/alphaorderly.py b/insert-interval/alphaorderly.py new file mode 100644 index 0000000000..6f1dbfe4ac --- /dev/null +++ b/insert-interval/alphaorderly.py @@ -0,0 +1,32 @@ +""" +시간 복잡도: O(N) +공간 복잡도: O(N) +- 새로운 배열을 만들어 리턴하기 떄문이다. + +기존 intervals 리스트에 새 interval을 삽입하여 겹치는 구간을 병합하는 코드입니다. +intervals는 이미 정렬되어 있다고 가정하며, +새로운 interval과의 겹침 여부를 판별하여 겹치면 병합하고, 그렇지 않으면 적절한 위치에 삽입합니다. +""" +class Solution: + def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]: + ans = [] + added = False + + for start, end in intervals: + if added: + ans.append([start, end]) + continue + + if end < newInterval[0]: + ans.append([start, end]) + elif start > newInterval[1]: + ans.append(newInterval) + ans.append([start, end]) + added = True + else: + newInterval = [min(start, newInterval[0]), max(newInterval[1], end)] + + if not added: + ans.append(newInterval) + + return ans diff --git a/kth-smallest-element-in-a-bst/alphaorderly.py b/kth-smallest-element-in-a-bst/alphaorderly.py new file mode 100644 index 0000000000..8644c3127d --- /dev/null +++ b/kth-smallest-element-in-a-bst/alphaorderly.py @@ -0,0 +1,33 @@ +""" +시간 복잡도: O(N) +공간 복잡도: O(N) + +이 코드는 이진 탐색 트리(BST)에서 k번째로 작은 값을 찾는 함수입니다. +중위 순회를 통해 노드를 오름차순으로 방문하여, +k번째 값을 찾는 방식으로 동작합니다. +""" +class Solution: + def kthSmallest(self, root: Optional[TreeNode], k: int) -> int: + stack = [] + index = 1 + + while root: + stack.append(root) + root = root.left + + while stack: + node = stack.pop() + + if index == k: + return node.val + index += 1 + + if not node.right: + continue + + right = node.right + while right: + stack.append(right) + right = right.left + + return -1 diff --git a/lowest-common-ancestor-of-a-binary-search-tree/alphaorderly.py b/lowest-common-ancestor-of-a-binary-search-tree/alphaorderly.py new file mode 100644 index 0000000000..22b98dc634 --- /dev/null +++ b/lowest-common-ancestor-of-a-binary-search-tree/alphaorderly.py @@ -0,0 +1,23 @@ +""" +시간 복잡도: O(N) +공간 복잡도: O(N) + +BST에서 두 노드의 최소 공통 조상을 찾는 코드입니다. +두 노드의 값을 비교하여 최소 공통 조상을 찾는 방식으로 동작합니다. +""" +class Solution: + def lowestCommonAncestor( + self, root: "TreeNode", p: "TreeNode", q: "TreeNode" + ) -> "TreeNode": + if root == p or root == q: + return root + + p_compare = root.val > p.val + q_compare = root.val > q.val + + if p_compare != q_compare: + return root + elif p_compare and q_compare: + return self.lowestCommonAncestor(root.left, p, q) + else: + return self.lowestCommonAncestor(root.right, p, q) diff --git a/meeting-rooms/alphaorderly.py b/meeting-rooms/alphaorderly.py new file mode 100644 index 0000000000..a51071e309 --- /dev/null +++ b/meeting-rooms/alphaorderly.py @@ -0,0 +1,23 @@ +""" +시간 복잡도: O(NLogN) + - intervals 리스트를 정렬하기 때문입니다. +공간 복잡도: O(N) + - 정렬 시 추가 메모리(새로운 배열)가 사용될 수 있습니다. + +주어진 intervals(회의 시간표)들이 서로 겹치는지 확인하는 코드입니다. +intervals를 시작 시간 순으로 정렬한 뒤, +이전 회의의 종료 시간(final)과 현재 회의의 시작 시간(start)을 비교하여 +회의가 겹치는 경우가 있는지 검사합니다. +""" +class Solution: + def canAttendMeetings(self, intervals: List[List[int]]) -> bool: + intervals.sort() + final = -1 + + for start, end in intervals: + if start >= final: + final = end + else: + return False + + return True