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.


Implement the StockPrice class:


	StockPrice() Initializes the object with no price records.
	void update(int timestamp, int price) Updates the price of the stock at the given timestamp.
	int current() Returns the latest price of the stock.
	int maximum() Returns the maximum price of the stock.
	int minimum() Returns the minimum price of the stock.


&nbsp;
Example 1:

Input
["StockPrice", "update", "update", "current", "maximum", "update", "maximum", "update", "minimum"]
[[], [1, 10], [2, 5], [], [], [1, 3], [], [4, 2], []]
Output
[null, null, null, 5, 10, null, 5, null, 2]

Explanation
StockPrice stockPrice = new StockPrice();
stockPrice.update(1, 10); // Timestamps are [1] with corresponding prices [10].
stockPrice.update(2, 5);  // Timestamps are [1,2] with corresponding prices [10,5].
stockPrice.current();     // return 5, the latest timestamp is 2 with the price being 5.
stockPrice.maximum();     // return 10, the maximum price is 10 at timestamp 1.
stockPrice.update(1, 3);  // The previous timestamp 1 had the wrong price, so it is updated to 3.
                          // Timestamps are [1,2] with corresponding prices [3,5].
stockPrice.maximum();     // return 5, the maximum price is 5 after the correction.
stockPrice.update(4, 2);  // Timestamps are [1,2,4] with corresponding prices [3,5,2].
stockPrice.minimum();     // return 2, the minimum price is 2 at timestamp 4.


&nbsp;
Constraints:


	1 &lt;= timestamp, price &lt;= 109
	At most 105 calls will be made in total to update, current, maximum, and minimum.
	current, maximum, and minimum will be called only after update has been called at least once.


class StockPrice:

    def __init__(self):
        self.timestamps = {}
        self.highestTimestamp = 0
        self.minHeap = []
        self.maxHeap = []

    def update(self, timestamp: int, price: int) -> None:
	    #Keep track of current prices
        self.timestamps[timestamp] = price
        self.highestTimestamp = max(self.highestTimestamp, timestamp)
        
		#For maximum/minimum
        heappush(self.minHeap, (price, timestamp))
        heappush(self.maxHeap, (-price, timestamp))

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

    def maximum(self) -> int:
        currPrice, timestamp = heappop(self.maxHeap)
		
		#If the price from the heap doesn't match the price the timestamp indicates, keep popping from the heap
        while -currPrice != self.timestamps[timestamp]:
            currPrice, timestamp = heappop(self.maxHeap)
            
        heappush(self.maxHeap, (currPrice, timestamp))
        return -currPrice

    def minimum(self) -> int:
        currPrice, timestamp = heappop(self.minHeap)
		
		#If the price from the heap doesn't match the price the timestamp indicates, keep popping from the heap
        while currPrice != self.timestamps[timestamp]:
            currPrice, timestamp = heappop(self.minHeap)
            
        heappush(self.minHeap, (currPrice, timestamp))
        return currPrice
class StockRecord {
    int timestamp;
    int price;
    
    public StockRecord(){}
    
    public StockRecord(int t, int p) {
        timestamp = t;
        price = p;
    }
}

class StockPrice {
    
    PriorityQueue<StockRecord> max = new PriorityQueue<>((sr1, sr2) -> (sr2.price - sr1.price));
    PriorityQueue<StockRecord> min = new PriorityQueue<>((sr1, sr2) -> (sr1.price - sr2.price));
    StockRecord current_record;
    Map<Integer, Integer> map = new HashMap<>();

    
    public StockPrice() {
        current_record = new StockRecord();
    }
    
    public void update(int timestamp, int price) {
        if(timestamp >= current_record.timestamp) {
            current_record.timestamp = timestamp;
            current_record.price = price;
        }
        
        StockRecord sr = new StockRecord(timestamp, price);
        max.add(sr);
        min.add(sr);
        map.put(timestamp, price);
    }
    
    public int current() {
        return current_record.price;
    }
    
    public int maximum() {
        StockRecord sp = max.peek();
        while(true) {
            sp = max.peek();
            if(sp.price != map.get(sp.timestamp))
                max.poll();
            else break;
        }
        return sp.price;
    }
    
    public int minimum() {
        StockRecord sp = min.peek();
         while(true) {
            sp = min.peek();
            if(sp.price != map.get(sp.timestamp))
                min.poll();
            else break;
        }
        return sp.price;
    }
}
class StockPrice {
    map<int, int> stock; 
    map<int, int> prices;
    pair<int, int> currentStock = {-1, -1};
public:
    
    StockPrice() {
        
    }
    
    void update(int timestamp, int price) {
	
		// Update current stock price if we get a new greater timestamp
        if(timestamp >= currentStock.first) {
            currentStock = {timestamp, price};
        }
        
        if(stock.find(timestamp) != stock.end()) {
			// Case 2
            int old_price = stock[timestamp];  // Get old price for timestamp
            
            prices[old_price]--;  // Reduce count 
            if(prices[old_price] == 0) prices.erase(old_price); // Remove if no timestamp has old price
        }
        
		// Case 1
        prices[price]++;  
        stocks[timestamp] = price;   
        
    }
    
    int current() {
        return currentStock.second;
    }
    
    int maximum() {
			return prices.rbegin()->first;  // GET last element
    }
    
    int minimum() {
        return prices.begin()->first; // Get first element
    }
};
var StockPrice = function() {
    this.stocksPrice=[]
    this.timesStamp=[]
    this.lastPrice=[0,0];
    this.maxPrice=Number.NEGATIVE_INFINITY
    this.minPrice=Number.POSITIVE_INFINITY
};

/** 
 * @param {number} timestamp 
 * @param {number} price
 * @return {void}
 */
StockPrice.prototype.update = function(timestamp, price) {  
    let index=this.timesStamp.indexOf(timestamp)
    if(index === -1) {
        this.timesStamp.push(timestamp)
        this.stocksPrice.push(price)
        this.maxPrice=Math.max(this.maxPrice, price)
        this.minPrice=Math.min(this.minPrice, price)
    } else {
        if(this.maxPrice === this.stocksPrice[index] || 
           this.minPrice === this.stocksPrice[index]) {
            this.stocksPrice[index]=price
            this.maxPrice=Math.max(...this.stocksPrice)
            this.minPrice=Math.min(...this.stocksPrice)
        } else {
            this.stocksPrice[index]=price
            this.maxPrice=Math.max(this.maxPrice, price)
            this.minPrice=Math.min(this.minPrice, price)
        }
    }
    
    if(timestamp >= this.lastPrice[0])  this.lastPrice=[timestamp, price] 
};

/**
 * @return {number}
 */
StockPrice.prototype.current = function() {
    return this.lastPrice[1];
};

/**
 * @return {number}
 */
StockPrice.prototype.maximum = function() {
    return this.maxPrice
};

/**
 * @return {number}
 */
StockPrice.prototype.minimum = function() {
    return this.minPrice
};

/** 
 * Your StockPrice object will be instantiated and called as such:
 * var obj = new StockPrice()
 * obj.update(timestamp,price)
 * var param_2 = obj.current()
 * var param_3 = obj.maximum()
 * var param_4 = obj.minimum()
 */
Stock Price Fluctuation