-
Notifications
You must be signed in to change notification settings - Fork 0
/
033.py
57 lines (43 loc) · 1.28 KB
/
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""
Problem:
Compute the running median of a sequence of numbers. That is, given a stream of
numbers, print out the median of the list so far on each new element.
Recall that the median of an even-numbered list is the average of the two middle
numbers.
For example, given the sequence [2, 1, 5, 7, 2, 0, 5], your algorithm should print out:
2
1.5
2
3.5
2
2
2
"""
from typing import List
from DataStructures.Heap import MaxHeap, MinHeap
def get_running_medians(arr: List[int]) -> List[int]:
min_heap = MinHeap()
max_heap = MaxHeap()
medians = []
for elem in arr:
# current median value generation
min_heap.insert(elem)
if len(min_heap) > len(max_heap) + 1:
smallest_large_element = min_heap.extract_min()
max_heap.insert(smallest_large_element)
if len(min_heap) == len(max_heap):
median = (min_heap.peek_min() + max_heap.peek_max()) / 2
else:
median = min_heap.peek_min()
medians.append(median)
return medians
if __name__ == "__main__":
print(get_running_medians([]))
print(get_running_medians([2, 5]))
print(get_running_medians([3, 3, 3, 3]))
print(get_running_medians([2, 1, 5, 7, 2, 0, 5]))
"""
SPECS:
TIME COMPLEXITY: O(n x log(n))
SPACE COMPLEXITY: O(n)
"""