-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2034. Stock Price Fluctuation.py
50 lines (33 loc) · 1.14 KB
/
2034. Stock Price Fluctuation.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
# 2034. Stock Price Fluctuation
import heapq
class StockPrice:
def __init__(self):
self.dict = {}
self.currtime = -math.inf
self.minheap = []
self.maxheap = []
def update(self, timestamp: int, price: int) -> None:
self.dict[timestamp] = price
self.currtime = max(timestamp, self.currtime)
heappush(self.maxheap, (-price, timestamp))
heappush(self.minheap, (price, timestamp))
def current(self) -> int:
return self.dict[self.currtime]
def maximum(self) -> int:
p, t = heappop(self.maxheap)
while -p != self.dict[t]:
p, t = heappop(self.maxheap)
heappush(self.maxheap, (-p,t))
return -p
def minimum(self) -> int:
p, t = heappop(self.minheap)
while p != self.dict[t]:
p, t = heappop(self.minheap)
heappush(self.minheap, (p,t))
return p
# Your StockPrice object will be instantiated and called as such:
# obj = StockPrice()
# obj.update(timestamp,price)
# param_2 = obj.current()
# param_3 = obj.maximum()
# param_4 = obj.minimum()