Leetcode LC-Contest

Leetcode 2034

Stock Price Fluctuation

Sailorlqh
2021-10-10
3 min

# 2034. Stock Price Fluctuation

Question link is here.

This is the third question of this week's weekly contest.

# Question

You are given a stream of records about a particular stock. Each record contains a timestamp and the corresponding price of the stock at that timestamp.

Unfortunately due to the volatile nature of the stock market, the records do not come in order. Even worse, some records may be incorrect. Another record with the same timestamp may appear later in the stream correcting the price of the previous wrong record.

Design an algorithm that:

  • Updates the price of the stock at a particular timestamp, correcting the price from any previous records at the timestamp.
  • Finds the latest price of the stock based on the current records. The latest price is the price at the latest timestamp recorded.
  • Finds the maximum price the stock has been based on the current records.
  • Finds the minimum price the stock has been based on the current records.

# Solution

We can use a max heap and a min heap to keep track of the max value and the min value. And use a variable current to keep track of the latest timestamp. And we can use a dict (hashmap) to store each timestamp's corresponding stock price.

But we need to pay attention to the update function, since the update of some timestamp might change the max or min value. Therefore, we would also need a priceCounter to store the count of each price. Therefore, if the some timestamp is modified, and if it is the max or min value, we can modify the max, min value as well.

# Python

class StockPrice:

    def __init__(self):
        self.log = {}
        self.Max = 10**9 + 1
        self.Min = 0
        self.MaxTimestamp = -1
        self.priceCounter = {}
        self.minHeap = []
        self.maxHeap = []
        
        

    def update(self, timestamp: int, price: int) -> None:
        if timestamp >= self.MaxTimestamp:
            self.MaxTimestamp = timestamp
        if(timestamp not in self.log.keys()):
            self.log[timestamp] = price
            heapq.heappush(self.minHeap, price)
            heapq.heappush(self.maxHeap, -price)
            self.priceCounter[price] = self.priceCounter.get(price, 0) + 1
            self.Min = self.minHeap[0]
            self.Max = -self.maxHeap[0]
        else:
            if self.log[timestamp] == price:
                return
            prevPrice = self.log[timestamp]
            self.log[timestamp] = price
            self.priceCounter[prevPrice] -= 1
            self.priceCounter[price] = self.priceCounter.get(price, 0) + 1
            heapq.heappush(self.minHeap, price)
            heapq.heappush(self.maxHeap, -price)
            if prevPrice == self.minHeap[0]:
                while self.priceCounter[self.minHeap[0]] == 0:
                    heapq.heappop(self.minHeap)
            if -prevPrice == self.maxHeap[0]:
                while self.priceCounter[-self.maxHeap[0]] == 0:
                    heapq.heappop(self.maxHeap)
            self.Max = -self.maxHeap[0]
            self.Min = self.minHeap[0]

    def current(self) -> int:
        return self.log[self.MaxTimestamp]
        
        
    def maximum(self) -> int:
        return self.Max
        

    def minimum(self) -> int:
        return self.Min