Hi abilityfun, you almost got it right except two things, one big mistake and one small mistake.

small mistake:

you are saving theap[0] in hashes, should be -theap[0], I think it just a typo.

big mistake:

In c++ by default, the priority_queue is a Max heap, "greater" type defines a Min heap, you should be careful when you are converting code from one language to another, maybe this is what confused you, in this problem, all values in the min heap should be greater than or equal to ones in the max heap.

class Solution(object):
def medianSlidingWindow(self, nums, k):
medians = []
hashes = collections.defaultdict(int)
bheap, theap = [], []
i = 0
while(i<k):
heappush(bheap, nums[i])
i+=1
for _ in range(k/2, 0, -1):
heappush(theap, -heappop(bheap))
while True:
if (k % 2):
medians.append(float(bheap[0]))
else:
medians.append((bheap[0]+-theap[0]) / 2.0)
if (i == len(nums)):
break
m, n, balance = nums[i-k], nums[i], 0
i+=1
if m >= bheap[0]:
balance-=1
if m == bheap[0]:
heappop(bheap)
else:
hashes[m]+=1
else:
balance+=1
if m == -theap[0]:
heappop(theap)
else:
hashes[m]+=1
if bheap and n >= bheap[0]:
balance+=1
heappush(bheap, n)
else:
balance-=1
heappush(theap,-n)
if balance < 0:
heappush(bheap,-heappop(theap))
elif balance > 0:
heappush(theap, -heappop(bheap))
while bheap and hashes[bheap[0]]:
hashes[heappop(bheap)]-=1
while theap and hashes[-theap[0]]:
hashes[-heappop(theap)]-=1
return medians