From 28866eb83303fcf07c85bc01f04eae814240c31b Mon Sep 17 00:00:00 2001 From: Yuseok Jo Date: Sat, 19 Sep 2026 01:24:37 +0900 Subject: [PATCH 1/6] lowest common ancestor of a binary search tree solution --- .../yuseok89.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 lowest-common-ancestor-of-a-binary-search-tree/yuseok89.py diff --git a/lowest-common-ancestor-of-a-binary-search-tree/yuseok89.py b/lowest-common-ancestor-of-a-binary-search-tree/yuseok89.py new file mode 100644 index 0000000000..9441d2c685 --- /dev/null +++ b/lowest-common-ancestor-of-a-binary-search-tree/yuseok89.py @@ -0,0 +1,18 @@ +# TC: O(H) +# SC: O(1) +# 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': + if root.val < p.val and root.val < q.val: + return self.lowestCommonAncestor(root.right, p, q) + elif root.val > p.val and root.val > q.val: + return self.lowestCommonAncestor(root.left, p, q) + else: + return root + From 46c37b9945d997bf8db3bdfbdb8727b35edbb388 Mon Sep 17 00:00:00 2001 From: Yuseok Jo Date: Sat, 19 Sep 2026 01:28:52 +0900 Subject: [PATCH 2/6] kth smallest element in a bst solution --- kth-smallest-element-in-a-bst/yuseok89.py | 32 +++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 kth-smallest-element-in-a-bst/yuseok89.py diff --git a/kth-smallest-element-in-a-bst/yuseok89.py b/kth-smallest-element-in-a-bst/yuseok89.py new file mode 100644 index 0000000000..39ddefdce3 --- /dev/null +++ b/kth-smallest-element-in-a-bst/yuseok89.py @@ -0,0 +1,32 @@ +# TC: O(N) +# SC: O(1) +# 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: TreeNode | None, k: int) -> int: + + ans = 0 + + def rec(node: TreeNode | None) -> None: + nonlocal k, ans + + if node is None or k <= 0: + return + + rec(node.left) + + k -= 1 + if k == 0: + ans = node.val + return + + rec(node.right) + + rec(root) + + return ans + From e9a16d7f96733fbfcf193987bc7c719589365756 Mon Sep 17 00:00:00 2001 From: Yuseok Jo Date: Sat, 19 Sep 2026 01:31:45 +0900 Subject: [PATCH 3/6] insert interval solution --- insert-interval/yuseok89.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 insert-interval/yuseok89.py diff --git a/insert-interval/yuseok89.py b/insert-interval/yuseok89.py new file mode 100644 index 0000000000..a5184096af --- /dev/null +++ b/insert-interval/yuseok89.py @@ -0,0 +1,24 @@ +#TD: O(1) +#SC: O(2) +class Solution: + def insert(self, intervals: list[list[int]], newInterval: list[int]) -> list[list[int]]: + + ans = [] + is_processed = False + + for interval in intervals: + if is_processed or interval[1] < newInterval[0]: + ans.append(interval) + elif newInterval[1] < interval[0]: + ans.append(newInterval) + ans.append(interval) + is_processed = True + else: + newInterval[0] = min(newInterval[0], interval[0]) + newInterval[1] = max(newInterval[1], interval[1]) + + if not ans or ans[-1][1] < newInterval[0]: + ans.append(newInterval) + + return ans + From 4a5585d75c43d8e75fc86b5ca245637efbd46d02 Mon Sep 17 00:00:00 2001 From: Yuseok Jo Date: Sat, 19 Sep 2026 01:34:28 +0900 Subject: [PATCH 4/6] find median from data atream solution --- find-median-from-data-stream/yuseok89.py | 49 ++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 find-median-from-data-stream/yuseok89.py diff --git a/find-median-from-data-stream/yuseok89.py b/find-median-from-data-stream/yuseok89.py new file mode 100644 index 0000000000..70cf0d84f1 --- /dev/null +++ b/find-median-from-data-stream/yuseok89.py @@ -0,0 +1,49 @@ +# TC (NlogN) +# SC (N) +class MedianFinder: + + def __init__(self): + self.max_heap = [] + self.min_heap = [] + + def addNum(self, num: int) -> None: + if len(self.min_heap) == 0 and len(self.max_heap) == 0: + heapq.heappush(self.min_heap, num) + elif len(self.max_heap) == 0: + mid = heapq.heappop(self.min_heap) + heapq.heappush(self.max_heap, -min(mid, num)) + heapq.heappush(self.min_heap, max(mid, num)) + elif len(self.max_heap) == len(self.min_heap): + mid1 = -self.max_heap[0] + mid2 = self.min_heap[0] + + if num <= mid1: + heapq.heappush(self.max_heap, -num) + else: + heapq.heappush(self.min_heap, num) + else: + if len(self.max_heap) > len(self.min_heap): + mid = -heapq.heappop(self.max_heap) + else: + mid = heapq.heappop(self.min_heap) + + heapq.heappush(self.max_heap, -min(mid, num)) + heapq.heappush(self.min_heap, max(mid, num)) + + def findMedian(self) -> float: + if len(self.max_heap) == len(self.min_heap): + mid1 = -self.max_heap[0] + mid2 = self.min_heap[0] + return (mid1 + mid2) / 2.0 + else: + if len(self.max_heap) > len(self.min_heap): + return float(-self.max_heap[0]) + else: + return float(self.min_heap[0]) + + +# Your MedianFinder object will be instantiated and called as such: +# obj = MedianFinder() +# obj.addNum(num) +# param_2 = obj.findMedian() + From f42006c70b323b5db5cb55fc7f3147d9396cce14 Mon Sep 17 00:00:00 2001 From: Yuseok Jo Date: Sat, 19 Sep 2026 20:44:17 +0900 Subject: [PATCH 5/6] =?UTF-8?q?=EB=A6=AC=EB=B7=B0=20=EB=B0=98=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- insert-interval/yuseok89.py | 2 +- lowest-common-ancestor-of-a-binary-search-tree/yuseok89.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/insert-interval/yuseok89.py b/insert-interval/yuseok89.py index a5184096af..1a94271d4d 100644 --- a/insert-interval/yuseok89.py +++ b/insert-interval/yuseok89.py @@ -1,4 +1,4 @@ -#TD: O(1) +#TC: O(1) #SC: O(2) class Solution: def insert(self, intervals: list[list[int]], newInterval: list[int]) -> list[list[int]]: diff --git a/lowest-common-ancestor-of-a-binary-search-tree/yuseok89.py b/lowest-common-ancestor-of-a-binary-search-tree/yuseok89.py index 9441d2c685..ddb2b8ae59 100644 --- a/lowest-common-ancestor-of-a-binary-search-tree/yuseok89.py +++ b/lowest-common-ancestor-of-a-binary-search-tree/yuseok89.py @@ -1,5 +1,5 @@ # TC: O(H) -# SC: O(1) +# SC: O(H) # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): From d37475c004a80069819dfdd953c1ef40595de60d Mon Sep 17 00:00:00 2001 From: Yuseok Jo Date: Sat, 19 Sep 2026 20:57:49 +0900 Subject: [PATCH 6/6] =?UTF-8?q?=EB=B3=B5=EC=9E=A1=EB=8F=84=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- insert-interval/yuseok89.py | 4 ++-- kth-smallest-element-in-a-bst/yuseok89.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/insert-interval/yuseok89.py b/insert-interval/yuseok89.py index 1a94271d4d..08c278fb89 100644 --- a/insert-interval/yuseok89.py +++ b/insert-interval/yuseok89.py @@ -1,5 +1,5 @@ -#TC: O(1) -#SC: O(2) +#TC: O(N) +#SC: O(N) class Solution: def insert(self, intervals: list[list[int]], newInterval: list[int]) -> list[list[int]]: diff --git a/kth-smallest-element-in-a-bst/yuseok89.py b/kth-smallest-element-in-a-bst/yuseok89.py index 39ddefdce3..71efa31e6d 100644 --- a/kth-smallest-element-in-a-bst/yuseok89.py +++ b/kth-smallest-element-in-a-bst/yuseok89.py @@ -1,5 +1,5 @@ # TC: O(N) -# SC: O(1) +# SC: O(H) # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None):