You are viewing a single comment's thread. Return to all comments →
Here is my python 2.7 version using heapq.
import heapq hmin = [] hmax = [] n = int(raw_input().strip()) for i in xrange(1, n+1): a_t = int(raw_input().strip()) if not hmax or a_t < abs(hmax[0]): heapq.heappush(hmax, -a_t) else: heapq.heappush(hmin, a_t) if len(hmin) - len(hmax) > 1: heapq.heappush(hmax, -heapq.heappop(hmin)) elif len(hmax) - len(hmin) > 1: heapq.heappush(hmin, -heapq.heappop(hmax)) if len(hmin) == len(hmax): print float(hmin[0] - hmax[0] ) / 2.0 elif len(hmin) < len(hmax): print float(-hmax[0]) else: print float(hmin[0])
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 →
Here is my python 2.7 version using heapq.