题解 | #数据流中的中位数#
数据流中的中位数
http://www.nowcoder.com/practice/9be0172896bd43948f8a32fb954e1be1
import heapq
class Solution:
def __init__(self):
self.maxheapq=[]
self.minheapq=[]
def Insert(self, num):
# write code here
if len(self.maxheapq)==len(self.minheapq):
heapq.heappush(self.minheapq,-heapq.heappushpop(self.maxheapq,-num))
else:
heapq.heappush(self.maxheapq,-heapq.heappushpop(self.minheapq,num))
def GetMedian(self):
# write code here
if len(self.maxheapq)==len(self.minheapq):
return (self.minheapq[0]-self.maxheapq[0])/2
return self.minheapq[0]