We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 92d9221 commit cb9dd7bCopy full SHA for cb9dd7b
find-median-from-data-stream/printjin-gmailcom.py
@@ -0,0 +1,15 @@
1
+import heapq
2
+
3
+class MedianFinder:
4
+ def __init__(self):
5
+ self.low = []
6
+ self.high = []
7
+ def addNum(self, num):
8
+ heapq.heappush(self.low, -num)
9
+ heapq.heappush(self.high, -heapq.heappop(self.low))
10
+ if len(self.high) > len(self.low):
11
+ heapq.heappush(self.low, -heapq.heappop(self.high))
12
+ def findMedian(self):
13
+ if len(self.low) > len(self.high):
14
+ return -self.low[0]
15
+ return (-self.low[0] + self.high[0]) / 2
0 commit comments