-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path295.py
72 lines (59 loc) · 2.55 KB
/
295.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
__________________________________________________________________________________________________
sample 196 ms submission
import heapq
class MedianFinder:
def __init__(self):
"""
initialize your data structure here.
"""
import heapq
self.maxh = [] # maxheap
self.minh = [] # minheap leave large number
def addNum(self, num: int) -> None:
if len(self.maxh) == len(self.minh):
tmp = heapq.heappushpop(self.maxh,-num) # 1 2 3
heapq.heappush(self.minh,-tmp) # 4 ,5 6 10 (mnum =10)
else:
tmp = heapq.heappushpop(self.minh,num) # 6 7 10 num = 7
heapq.heappush(self.maxh,-tmp) # 1 2 3 4
def findMedian(self) -> float:
if len(self.maxh) == len(self.minh):
return (-self.maxh[0] + self.minh[0])/2
else:
return self.minh[0]
# Your MedianFinder object will be instantiated and called as such:
# obj = MedianFinder()
# obj.addNum(num)
# param_2 = obj.findMedian()
__________________________________________________________________________________________________
sample 23208 kb submission
class MedianFinder:
def __init__(self):
"""
initialize your data structure here.
"""
self.left_heap = [] # max heap - minheap with -val, lower half
self.right_heap = [] # min heap - upper half
def addNum(self, num: int) -> None:
# add to heap based on value
# make sure heap sizes meet constraint: size(left heap) = size(right heap) (- 1)
if len(self.right_heap) == 0 or num > self.right_heap[0]:
heapq.heappush(self.right_heap, num)
while len(self.left_heap) + 1 < len(self.right_heap):
val = heapq.heappop(self.right_heap)
heapq.heappush(self.left_heap, -val)
else:
heapq.heappush(self.left_heap, -num)
while len(self.left_heap) > len(self.right_heap):
val = heapq.heappop(self.left_heap)
heapq.heappush(self.right_heap, -val)
return
def findMedian(self) -> float:
if len(self.left_heap) == len(self.right_heap):
return (-self.left_heap[0] + self.right_heap[0]) / 2
return self.right_heap[0]
# Your MedianFinder object will be instantiated and called as such:
# obj = MedianFinder()
# obj.addNum(num)
# param_2 = obj.findMedian()
__________________________________________________________________________________________________