Skip to content

Commit 4673e0f

Browse files
authored
Update Solution.py
1 parent 6b9fc50 commit 4673e0f

File tree

1 file changed

+9
-12
lines changed
  • solution/0200-0299/0295.Find Median from Data Stream

1 file changed

+9
-12
lines changed

solution/0200-0299/0295.Find Median from Data Stream/Solution.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
class MedianFinder:
2+
23
def __init__(self):
3-
"""
4-
initialize your data structure here.
5-
"""
6-
self.h1 = []
7-
self.h2 = []
4+
self.minq = []
5+
self.maxq = []
86

97
def addNum(self, num: int) -> None:
10-
heappush(self.h1, num)
11-
heappush(self.h2, -heappop(self.h1))
12-
if len(self.h2) - len(self.h1) > 1:
13-
heappush(self.h1, -heappop(self.h2))
8+
heappush(self.minq, -heappushpop(self.maxq, -num))
9+
if len(self.minq) - len(self.maxq) > 1:
10+
heappush(self.maxq, -heappop(self.minq))
1411

1512
def findMedian(self) -> float:
16-
if len(self.h2) > len(self.h1):
17-
return -self.h2[0]
18-
return (self.h1[0] - self.h2[0]) / 2
13+
if len(self.minq) == len(self.maxq):
14+
return (self.minq[0] - self.maxq[0]) / 2
15+
return self.minq[0]
1916

2017

2118
# Your MedianFinder object will be instantiated and called as such:

0 commit comments

Comments
 (0)