-
Notifications
You must be signed in to change notification settings - Fork 0
/
median_finder.py
42 lines (32 loc) · 1.13 KB
/
median_finder.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
import heapq
class MedianFinder:
def __init__(self):
self.heaps = [], []
def addNum(self, num: int) -> None:
small, large = self.heaps
heapq.heappush(small, -heapq.heappushpop(large, num))
if len(large) < len(small):
heapq.heappush(large, -heapq.heappop(small))
def findMedian(self) -> float:
small, large = self.heaps
if len(large) > len(small):
return float(large[0])
return (large[0] - small[0]) / 2.0
# Time Complexity: O(nlogn)
# Space Complexity: O(n)
# Your MedianFinder object will be instantiated and called as such:
# obj = MedianFinder()
# obj.addNum(num)
# param_2 = obj.findMedian()
import unittest
class TestMedianFinder(unittest.TestCase):
def setUp(self) -> None:
self.median_finder = MedianFinder()
def test_median_finder(self) -> None:
self.median_finder.addNum(1)
self.median_finder.addNum(2)
self.assertEqual(self.median_finder.findMedian(), 1.5)
self.median_finder.addNum(3)
self.assertEqual(self.median_finder.findMedian(), 2)
if __name__ == '__main__':
unittest.main()