1
class StockPrice:
2

3
def __init__(self):
4
self.timestamps = {}
5
self.highestTimestamp = 0
6
self.minHeap = []
7
self.maxHeap = []
8

9
def update(self, timestamp: int, price: int) -> None:
10
# Keep track of current prices
11
self.timestamps[timestamp] = price
12
self.highestTimestamp = max(self.highestTimestamp, timestamp)
13

14
# For maximum/minimum
15
heappush(self.minHeap, (price, timestamp))
16
heappush(self.maxHeap, (-price, timestamp))
17

18
def current(self) -> int:
19
# Just return the highest timestamp in O(1)
20
return self.timestamps[self.highestTimestamp]
21

22
def maximum(self) -> int:
23
currPrice, timestamp = heappop(self.maxHeap)
24

25
# If the price from the heap doesn't match the price the timestamp indicates, keep popping from the heap
26
while -currPrice != self.timestamps[timestamp]:
27
currPrice, timestamp = heappop(self.maxHeap)
28

29
heappush(self.maxHeap, (currPrice, timestamp))
30
return -currPrice
31

32
def minimum(self) -> int:
33
currPrice, timestamp = heappop(self.minHeap)
34

35
# If the price from the heap doesn't match the price the timestamp indicates, keep popping from the heap
36
while currPrice != self.timestamps[timestamp]:
37
currPrice, timestamp = heappop(self.minHeap)
38

39
heappush(self.minHeap, (currPrice, timestamp))
40
return currPrice

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0