-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path18.9.median_of_input_stream.py
More file actions
40 lines (32 loc) · 1 KB
/
Copy path18.9.median_of_input_stream.py
File metadata and controls
40 lines (32 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import heapq
class MedianFinder:
def __init__(self):
self.maxHeap = []
self.minHeap = []
def addNumber(self, num):
if len(self.maxHeap) == len(self.minHeap):
if self.minHeap and num > self.minHeap[0]:
heapq.heappush(self.maxHeap, -heapq.heappop(self.minHeap))
heapq.heappush(self.minHeap, num)
else:
heapq.heappush(self.maxHeap, -num)
else:
if num < -self.maxHeap[0]:
heapq.heappush(self.minHeap, -heapq.heappop(self.maxHeap))
heapq.heappush(self.maxHeap, -num)
else:
heapq.heappush(self.minHeap, num)
def getMedian(self):
print self.maxHeap, self.minHeap
if not self.maxHeap:
return 0
if len(self.maxHeap) is len(self.minHeap):
return (-heapq.heappop(self.maxHeap) + heapq.heappop(self.minHeap)) / 2
else:
return -heapq.heappop(self.maxHeap)
if __name__ == '__main__':
stream = [1,2,3,4,5,6,7,8,9,10,-11]
m = MedianFinder()
for n in stream:
m.addNumber(n)
print m.getMedian()