Skip to content

Commit cb9dd7b

Browse files
Solve : Find Median From Data Stream
1 parent 92d9221 commit cb9dd7b

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)