-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathfind-median-from-data-stream.py
More file actions
38 lines (29 loc) · 1010 Bytes
/
Copy pathfind-median-from-data-stream.py
File metadata and controls
38 lines (29 loc) · 1010 Bytes
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
import heapq
class MedianFinder:
def __init__(self):
"""
initialize your data structure here.
"""
self.heap_left = []
self.heap_right = []
def addNum(self, num: int) -> None:
if len(self.heap_left) == len(self.heap_right):
heapq.heappush(self.heap_left, - num)
else:
heapq.heappush(self.heap_right, num)
if not len(self.heap_right):
return
left = - self.heap_left[0]
right = self.heap_right[0]
if left > right:
heapq.heapreplace(self.heap_left, - right)
heapq.heapreplace(self.heap_right, left)
def findMedian(self) -> float:
if len(self.heap_left) == len(self.heap_right):
return (- self.heap_left[0] + self.heap_right[0]) / 2.0
else:
return - self.heap_left[0]
# Your MedianFinder object will be instantiated and called as such:
# obj = MedianFinder()
# obj.addNum(num)
# param_2 = obj.findMedian()