from heapq
import *
class MedianFinder:
def __init__(self
):
"""
initialize your data structure here.
"""
self
.a
=[]
self
.b
=[]
def addNum(self
, num
: int) -> None:
if len(self
.a
)==len(self
.b
):
heappush
(self
.b
,-num
)
heappush
(self
.a
,-heappop
(self
.b
))
else:
heappush
(self
.a
,num
)
heappush
(self
.b
,-heappop
(self
.a
))
def findMedian(self
) -> float:
if len(self
.a
)==len(self
.b
): return (heappop
(self
.a
)-heappop
(self
.b
))/2
else: return heappop
(self
.a
)
转载请注明原文地址:https://tech.qufami.com/read-17017.html