File tree Expand file tree Collapse file tree 1 file changed +9
-12
lines changed
solution/0200-0299/0295.Find Median from Data Stream Expand file tree Collapse file tree 1 file changed +9
-12
lines changed Original file line number Diff line number Diff line change 1
1
class MedianFinder :
2
+
2
3
def __init__ (self ):
3
- """
4
- initialize your data structure here.
5
- """
6
- self .h1 = []
7
- self .h2 = []
4
+ self .minq = []
5
+ self .maxq = []
8
6
9
7
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 ))
14
11
15
12
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 ]
19
16
20
17
21
18
# Your MedianFinder object will be instantiated and called as such:
You can’t perform that action at this time.
0 commit comments