-
-
Notifications
You must be signed in to change notification settings - Fork 50.5k
Expand file tree
/
Copy pathmedian_in_a_stream.py
More file actions
97 lines (77 loc) · 2.22 KB
/
median_in_a_stream.py
File metadata and controls
97 lines (77 loc) · 2.22 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import heapq
def signum(a: int, b: int) -> int:
"""
Compare two integers.
Returns:
1 if a > b
-1 if a < b
0 if a == b
"""
if a > b:
return 1
if a < b:
return -1
return 0
def call_median(
element: int,
max_heap: list[int],
min_heap: list[int],
median: int,
) -> int:
"""
Insert an element into heaps and update the median.
"""
case = signum(len(max_heap), len(min_heap))
if case == 0:
if element > median:
heapq.heappush(min_heap, element)
median = min_heap[0]
else:
heapq.heappush(max_heap, -element)
median = -max_heap[0]
elif case == 1:
if element > median:
heapq.heappush(min_heap, element)
else:
heapq.heappush(min_heap, -heapq.heappop(max_heap))
heapq.heappush(max_heap, -element)
median = (-max_heap[0] + min_heap[0]) // 2
else:
if element > median:
heapq.heappush(max_heap, -heapq.heappop(min_heap))
heapq.heappush(min_heap, element)
else:
heapq.heappush(max_heap, -element)
median = (-max_heap[0] + min_heap[0]) // 2
return median
def median_in_a_stream(numbers: list[int]) -> list[int]:
"""
Find the median after each insertion in a stream of integers.
Uses two heaps and follows the classic running median logic.
Reference:
https://en.wikipedia.org/wiki/Median#Running_median
Args:
numbers: List of integers
Returns:
List of medians after each insertion
Raises:
ValueError: If the input list is empty
>>> median_in_a_stream([20, 14, 13, 16, 17])
[20, 17, 14, 15, 16]
>>> median_in_a_stream([5, 15, 1, 3])
[5, 10, 5, 4]
>>> median_in_a_stream([])
Traceback (most recent call last):
...
ValueError: Input list must not be empty
"""
if not numbers:
raise ValueError("Input list must not be empty")
max_heap: list[int] = []
min_heap: list[int] = []
median = 0
result: list[int] = []
for element in numbers:
median = call_median(element, max_heap, min_heap, median)
result.append(median)
return result