2034. Stock Price Fluctuation (Medium) - TengnanYao/daily_leetcode GitHub Wiki

class StockPrice:

    def __init__(self):
        self.price = {}
        self.max = []
        self.min = []
        self.cur = [0, 0]

    def update(self, timestamp: int, price: int) -> None:
        if timestamp >= self.cur[0]:
            self.cur = [timestamp, price]
        self.price[timestamp] = price
        heapq.heappush(self.max, (-price, timestamp))
        heapq.heappush(self.min, (price, timestamp))
        while self.price[self.max[0][1]] != -self.max[0][0]:
            heapq.heappop(self.max)
        while self.price[self.min[0][1]] != self.min[0][0]:
            heapq.heappop(self.min)

    def current(self) -> int:
        return self.cur[1]

    def maximum(self) -> int:
        return -self.max[0][0]

    def minimum(self) -> int:
        return self.min[0][0]

# 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()