forked from ngiengkianyew/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 1
/
problem_033.py
31 lines (23 loc) · 820 Bytes
/
problem_033.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import heapq as hq
def get_running_medians(arr):
if not arr:
return None
min_heap = list()
max_heap = list()
medians = list()
for x in arr:
hq.heappush(min_heap, x)
if len(min_heap) > len(max_heap) + 1:
smallest_large_element = hq.heappop(min_heap)
hq.heappush(max_heap, -smallest_large_element)
if len(min_heap) == len(max_heap):
median = (min_heap[0] - max_heap[0]) / 2
else:
median = min_heap[0]
medians.append(median)
return medians
assert not get_running_medians(None)
assert not get_running_medians([])
assert get_running_medians([2, 5]) == [2, 3.5]
assert get_running_medians([3, 3, 3, 3]) == [3, 3, 3, 3]
assert get_running_medians([2, 1, 5, 7, 2, 0, 5]) == [2, 1.5, 2, 3.5, 2, 2, 2]