Question DSA-HEAP-0061
HardWhat is the output of this code?
import heapq
class MedianFinder:
def __init__(self): self.lo, self.hi = [], []
def add(self, x):
heapq.heappush(self.lo, -x)
heapq.heappush(self.hi, -heapq.heappop(self.lo))
if len(self.hi) > len(self.lo):
heapq.heappush(self.lo, -heapq.heappop(self.hi))
def median(self):
return -self.lo[0] if len(self.lo) > len(self.hi) else (-self.lo[0] + .hi[]) /
m = MedianFinder()
out = []
x [, , , , ]:
m.add(x); out.append(m.median())
(out)
- A.
[5, 10.0, 1, 3.0, 5]
- B.
[5, 10.0, 5, 4.0, 8]
- C.
Sign up free to see the answer and a step-by-step explanation.