-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathq0295.py
More file actions
38 lines (31 loc) · 818 Bytes
/
q0295.py
File metadata and controls
38 lines (31 loc) · 818 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
#!/usr/bin/python3
from typing import List
from heapq import heappush, heappushpop
class MedianFinder:
count = 0
left = []
right = []
def __init__(self):
"""
initialize your data structure here.
"""
self.count = 0
self.left = []
self.right = []
def addNum(self, num: int) -> None:
if self.count % 2 == 0:
heappush(self.right, -heappushpop(self.left, -num))
else:
heappush(self.left, -heappushpop(self.right, num))
self.count += 1
def findMedian(self) -> float:
if self.count % 2 == 1:
return self.right[0]
else:
return (-self.left[0] + self.right[0]) / 2.0
heap1 = []
heappush(heap1,2)
heappush(heap1,1)
heappush(heap1,3)
print(heap1[0])
print(heap1)