-
-
Notifications
You must be signed in to change notification settings - Fork 358
[dolphinflow86] WEEK 13 Solutions #2864
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
f9f30a8
caf7080
1402650
5528633
967a5c4
f538d65
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # N is the number of elements added to the data stream. | ||
| # TC: O(log N) for addNum, O(1) for findMedian | ||
| # SC: O(N) - stores elements split into two heaps | ||
|
|
||
| import heapq | ||
|
|
||
|
|
||
| class MedianFinder: | ||
|
|
||
| def __init__(self): | ||
| self.small = [] # max-heap (store negative values) | ||
| self.large = [] # min-heap | ||
|
|
||
| def addNum(self, num: int) -> None: | ||
| heapq.heappush(self.small, -num) | ||
|
|
||
| if self.small and self.large and (-self.small[0] > self.large[0]): | ||
| val = -heapq.heappop(self.small) | ||
| heapq.heappush(self.large, val) | ||
|
|
||
| if len(self.small) > len(self.large) + 1: | ||
| val = -heapq.heappop(self.small) | ||
| heapq.heappush(self.large, val) | ||
|
|
||
| if len(self.large) > len(self.small): | ||
| val = heapq.heappop(self.large) | ||
| heapq.heappush(self.small, -val) | ||
|
|
||
| def findMedian(self) -> float: | ||
| if len(self.small) > len(self.large): | ||
| return float(-self.small[0]) | ||
| return (-self.small[0] + self.large[0]) / 2.0 |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석insert-interval/dolphinflow86.py# N is the number of intervals.
# TC: O(N) - single pass through intervals array
# SC: O(N) - stores result intervals list
class Solution:
def insert(self, intervals, newInterval):
result = []
i = 0
n = len(intervals)
while i < n and intervals[i][1] < newInterval[0]:
result.append(intervals[i])
i += 1
while i < n and intervals[i][0] <= newInterval[1]:
newInterval[0] = min(newInterval[0], intervals[i][0])
newInterval[1] = max(newInterval[1], intervals[i][1])
i += 1
result.append(newInterval)
while i < n:
result.append(intervals[i])
i += 1
return result
📊 시간/공간 복잡도 분석
피드백: 왼쪽, 중간, 오른쪽 파트를 순차적으로 처리하여 중복되는 구간을 하나의 구간으로 합칩니다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| # N is the number of intervals. | ||
| # TC: O(N) - single pass through intervals array | ||
| # SC: O(N) - stores result intervals list | ||
|
|
||
|
|
||
| class Solution: | ||
|
|
||
| def insert(self, intervals, newInterval): | ||
| result = [] | ||
| i = 0 | ||
| n = len(intervals) | ||
|
|
||
| while i < n and intervals[i][1] < newInterval[0]: | ||
| result.append(intervals[i]) | ||
| i += 1 | ||
|
|
||
| while i < n and intervals[i][0] <= newInterval[1]: | ||
| newInterval[0] = min(newInterval[0], intervals[i][0]) | ||
| newInterval[1] = max(newInterval[1], intervals[i][1]) | ||
| i += 1 | ||
|
|
||
| result.append(newInterval) | ||
|
|
||
| while i < n: | ||
| result.append(intervals[i]) | ||
| i += 1 | ||
|
|
||
| return result |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석kth-smallest-element-in-a-bst/dolphinflow86.py# H is the height of the BST, and K is the target rank.
# TC: O(H + K) - in-order traversal stops after visiting K elements
# SC: O(H) - stack memory for in-order traversal recursion/loop
# 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, k: int) -> int:
stack = []
curr = root
while curr or stack:
while curr:
stack.append(curr)
curr = curr.left
curr = stack.pop()
k -= 1
if k == 0:
return curr.val
curr = curr.right
return -1
📊 시간/공간 복잡도 분석
피드백: BST의 성질을 이용해 왼쪽으로 내려가며 순서를 카운트합니다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # H is the height of the BST, and K is the target rank. | ||
| # TC: O(H + K) - in-order traversal stops after visiting K elements | ||
| # SC: O(H) - stack memory for in-order traversal recursion/loop | ||
|
|
||
| # 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, k: int) -> int: | ||
| stack = [] | ||
| curr = root | ||
|
|
||
| while curr or stack: | ||
| while curr: | ||
| stack.append(curr) | ||
| curr = curr.left | ||
|
|
||
| curr = stack.pop() | ||
| k -= 1 | ||
|
|
||
| if k == 0: | ||
| return curr.val | ||
|
|
||
| curr = curr.right | ||
|
|
||
| return -1 |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석lowest-common-ancestor-of-a-binary-search-tree/dolphinflow86.py# H is the height of the binary search tree.
# TC: O(H) - traverses down tree height
# SC: O(1) - uses iterative traversal without recursion stack
# 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, p, q):
curr = root
while curr:
if p.val < curr.val and q.val < curr.val:
curr = curr.left
elif p.val > curr.val and q.val > curr.val:
curr = curr.right
else:
return curr
return None
📊 시간/공간 복잡도 분석
피드백: 현재 노드 값을 비교해 좌우로 내려가며 LCA를 찾습니다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # H is the height of the binary search tree. | ||
| # TC: O(H) - traverses down tree height | ||
| # SC: O(1) - uses iterative traversal without recursion stack | ||
|
|
||
| # 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, p, q): | ||
| curr = root | ||
|
|
||
| while curr: | ||
| if p.val < curr.val and q.val < curr.val: | ||
| curr = curr.left | ||
| elif p.val > curr.val and q.val > curr.val: | ||
| curr = curr.right | ||
| else: | ||
| return curr | ||
|
|
||
| return None |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| # n is the number of nodes, and E is the number of edges. | ||
| # TC: O(V + E * alpha(V)) - near linear time with path compression Union-Find | ||
| # SC: O(V) - parent and rank arrays for Union-Find | ||
|
|
||
|
|
||
| class Solution: | ||
|
|
||
| def countComponents(self, n: int, edges) -> int: | ||
| parent = list(range(n)) | ||
| rank = [1] * n | ||
|
|
||
| def find(node): | ||
| if parent[node] != node: | ||
| parent[node] = find(parent[node]) | ||
| return parent[node] | ||
|
|
||
| def union(n1, n2): | ||
| p1, p2 = find(n1), find(n2) | ||
| if p1 == p2: | ||
| return 0 | ||
|
|
||
| if rank[p1] > rank[p2]: | ||
| parent[p2] = p1 | ||
| rank[p1] += rank[p2] | ||
| else: | ||
| parent[p1] = p2 | ||
| rank[p2] += rank[p1] | ||
| return 1 | ||
|
|
||
| components = n | ||
| for u, v in edges: | ||
| components -= union(u, v) | ||
|
|
||
| return components |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| # N is the number of nodes in the binary tree. | ||
| # TC: O(N) - visits each node once during serialization and deserialization | ||
| # SC: O(N) - stores node values and recursion stack | ||
|
|
||
| # Definition for a binary tree node. | ||
| # class TreeNode(object): | ||
| # def __init__(self, x): | ||
| # self.val = x | ||
| # self.left = None | ||
| # self.right = None | ||
|
|
||
|
|
||
| class Codec: | ||
|
|
||
| def serialize(self, root): | ||
| vals = [] | ||
|
|
||
| def dfs(node): | ||
| if not node: | ||
| vals.append("N") | ||
| return | ||
| vals.append(str(node.val)) | ||
| dfs(node.left) | ||
| dfs(node.right) | ||
|
|
||
| dfs(root) | ||
| return ",".join(vals) | ||
|
|
||
| def deserialize(self, data): | ||
| vals = data.split(",") | ||
| self.i = 0 | ||
|
|
||
| def dfs(): | ||
| if vals[self.i] == "N": | ||
| self.i += 1 | ||
| return None | ||
|
|
||
| node = TreeNode(int(vals[self.i])) | ||
| self.i += 1 | ||
| node.left = dfs() | ||
| node.right = dfs() | ||
| return node | ||
|
|
||
| return dfs() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🏷️ 알고리즘 패턴 분석
find-median-from-data-stream/dolphinflow86.py
📊 시간/공간 복잡도 분석
풀이 1:
MedianFinder.addNum— Time: O(log n) / Space: O(n)피드백: 두 개의 힙으로 삽입 시나리오를 균형 있게 유지해 최악의 경우에도 로그 시간에 가운데값을 재조정합니다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 2:
MedianFinder.findMedian— Time: O(1) / Space: O(n)피드백: 상황에 따라 두 힙의 최댓값/최솟값을 활용해 상호 보완적으로 중간값을 계산합니다.
개선 제안: 현재 구현이 적절해 보입니다.