You are viewing a single comment's thread. Return to all comments →
yeah, that's the fastest solution:
from heapq import heappush as push, heappushpop as pushpop class Spliter: def __init__(self): self.upper = [] self.lower = [] def median(self): if len(self.upper) > len(self.lower): return self.upper[0] else: return (self.upper[0] - self.lower[0]) / 2. def add(self, value): value = pushpop(self.upper, value) value = -pushpop(self.lower, -value) if len(self.upper) <= len(self.lower): push(self.upper, value) else: push(self.lower, -value)
but, of course, probably you want the DIY version for an interview.
Seems like cookies are disabled on this browser, please enable them to open this website
Heaps: Find the Running Median
You are viewing a single comment's thread. Return to all comments →
yeah, that's the fastest solution:
but, of course, probably you want the DIY version for an interview.