mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-06-19 12:24:11 -04:00
Create median_from_stream.py
This commit is contained in:
parent
8509c2f7a1
commit
296c30779b
1 changed files with 37 additions and 0 deletions
37
heaps/median_from_stream.py
Normal file
37
heaps/median_from_stream.py
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# author: bt3gl
|
||||||
|
|
||||||
|
|
||||||
|
class MedianFinder:
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
|
||||||
|
self.min_heap = []
|
||||||
|
self.max_heap = []
|
||||||
|
|
||||||
|
|
||||||
|
def addNum(self, num: int) -> None:
|
||||||
|
|
||||||
|
min_heap, max_heap = self.min_heap, self.max_heap
|
||||||
|
|
||||||
|
if len(min_heap) < len(max_heap):
|
||||||
|
heappush(min_heap, num)
|
||||||
|
|
||||||
|
else:
|
||||||
|
heappush(max_heap, -num)
|
||||||
|
|
||||||
|
if max_heap and min_heap and -max_heap[0] > min_heap[0]:
|
||||||
|
|
||||||
|
heappush(max_heap, -heappop(min_heap))
|
||||||
|
heappush(min_heap, -heappop(max_heap))
|
||||||
|
|
||||||
|
|
||||||
|
def findMedian(self):
|
||||||
|
|
||||||
|
min_heap, max_heap = self.min_heap, self.max_heap
|
||||||
|
|
||||||
|
if len(min_heap) < len(max_heap):
|
||||||
|
return -max_heap[0]
|
||||||
|
|
||||||
|
return (min_heap[0] - max_heap[0]) / 2
|
Loading…
Add table
Add a link
Reference in a new issue