Skip to content

[printjin-gmailcom] Week 13 Solutions #1610

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
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
15 changes: 15 additions & 0 deletions find-median-from-data-stream/printjin-gmailcom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import heapq

class MedianFinder:
def __init__(self):
self.low = []
self.high = []
def addNum(self, num):
heapq.heappush(self.low, -num)
heapq.heappush(self.high, -heapq.heappop(self.low))
if len(self.high) > len(self.low):
heapq.heappush(self.low, -heapq.heappop(self.high))
def findMedian(self):
if len(self.low) > len(self.high):
return -self.low[0]
return (-self.low[0] + self.high[0]) / 2
17 changes: 17 additions & 0 deletions insert-interval/printjin-gmailcom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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
15 changes: 15 additions & 0 deletions kth-smallest-element-in-a-bst/printjin-gmailcom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution:
def kthSmallest(self, root, k):
self.count = 0
self.result = 0
def inorder(node):
if not node:
return
inorder(node.left)
self.count += 1
if self.count == k:
self.result = node.val
return
inorder(node.right)
inorder(root)
return self.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Solution:
def lowestCommonAncestor(self, root, p, q):
while root:
if p.val < root.val and q.val < root.val:
root = root.left
elif p.val > root.val and q.val > root.val:
root = root.right
else:
return root
9 changes: 9 additions & 0 deletions meeting-rooms/printjin-gmailcom,.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from typing import List

class Solution:
def canAttendMeetings(self, intervals: List[Interval]) -> bool:
intervals.sort(key=lambda x: x.start)
for i in range(1, len(intervals)):
if intervals[i].start < intervals[i - 1].end:
return False
return True