We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
defrunningMedian(a):# Declaring two min heap# import at the top# from heapq import heappush, heappop, heapifyg=[]s=[]result=[]foriinrange(len(a)):# Negation for treating it as max heapheappush(s,-a[i])heappush(g,-heappop(s))iflen(g)>len(s):heappush(s,-heappop(g))iflen(g)!=len(s):result.append(float(-s[0]))else:result.append(float((g[0]-s[0])/2))returnresult
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Find the Running Median
You are viewing a single comment's thread. Return to all comments →
My Python solution using two heaps.