Skip to content
Open
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 find-median-from-data-stream/8804who.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import heapq

class MedianFinder:

def __init__(self):
self.max_heap = []
self.min_heap = []

def addNum(self, num: int) -> None:
if not self.min_heap or self.min_heap[0] <= num:
heapq.heappush(self.min_heap, num)
else:
heapq.heappush(self.max_heap, -num)

if len(self.min_heap) < len(self.max_heap):
heapq.heappush(self.min_heap, -heapq.heappop(self.max_heap))
elif len(self.min_heap) > len(self.max_heap) + 1:
heapq.heappush(self.max_heap, -heapq.heappop(self.min_heap))

def findMedian(self) -> float:
if len(self.min_heap) == len(self.max_heap):
return (self.min_heap[0]-self.max_heap[0])/2
else:
return self.min_heap[0]


20 changes: 20 additions & 0 deletions insert-interval/8804who.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution:
def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
new_start = 1e9
new_end = -1

answer = []

for start, end in intervals:
if start <= newInterval[0] <= end or newInterval[0] <= start <= newInterval[1] or newInterval[0] <= end <= newInterval[1] or start <= newInterval[1] <= end:
if new_start == 1e9:
new_start = start
new_end = end
else:
answer.append([start, end])
new_start = min(new_start, newInterval[0])
new_end = max(new_end, newInterval[1])

answer.append([new_start, new_end])
return sorted(answer)
Copy link
Member

Choose a reason for hiding this comment

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

입력 intervals가 이미 정렬되어 있다는 조건이 주어져 있으므로, 정렬 없이 올바른 위치에 삽입하면 O(n)으로 풀 수 있을 것 같습니다.


25 changes: 25 additions & 0 deletions kth-smallest-element-in-a-bst/8804who.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 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:
answer = 0

def dfs(node):
nonlocal k
nonlocal answer
if node.left:
dfs(node.left)
k -= 1
if k == 0:
answer = node.val
return
if node.right:
dfs(node.right)
dfs(root)

return answer

15 changes: 15 additions & 0 deletions lowest-common-ancestor-of-a-binary-search-tree/8804who.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# 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 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