Leetcode 2034. Stock price fluctuation__ Dual set storage

Posted by T_Hayden on Mon, 24 Jan 2022 08:23:46 +0100

2034. Stock price fluctuation

Give you a data stream of stock prices. Each record in the data stream contains a time stamp and the price corresponding to the stock at that time point.

Unfortunately, due to the inherent volatility of the stock market, stock price records may not come in chronological order. In some cases, some records may be wrong. If two records with the same time stamp appear in the data stream, the previous record is regarded as an error record, and the later record corrects the previous error record.

Please design an algorithm to realize:

Update the stock price of the stock at a certain time stamp. If there is a price with the same time stamp before, this operation will correct the previous wrong price.
Find the latest stock price in the current record. The latest stock price is defined as the stock price with the latest timestamp.
Find the highest price of the stock in the current record.
Find the lowest price of the stock in the current record.
Please implement the StockPrice class:

StockPrice() initializes the object. Currently, there is no stock price record.
void update(int timestamp, int price) updates the stock price to price at the time point timestamp.
int current() returns the latest stock price.
int maximum() returns the highest stock price.
int minimum() returns the lowest stock price.

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); // The timestamp is [1], and the corresponding stock price is [10].
stockPrice.update(2, 5);  // The timestamp is [1,2], and the corresponding stock price is [10,5].
stockPrice.current();     // Returns 5. The latest timestamp is 2 and the corresponding price is 5.
stockPrice.maximum();     // Returns 10. The timestamp of the highest price is 1 and the price is 10.
stockPrice.update(1, 3);  // The previous price with timestamp of 1 is wrong, and the price is updated to 3.
                          // The timestamp is [1,2], and the corresponding stock price is [3,5].
stockPrice.maximum();     // Return to 5, and the corrected maximum price is 5.
stockPrice.update(4, 2);  // The timestamp is [1,2,4], and the corresponding price is [3,5,2].
stockPrice.minimum();     // Returns 2, the lowest price timestamp is 4, and the price is 2.
 

Tips:

1 <= timestamp, price <= 109
update,current,maximum and minimum The total number of calls shall not exceed 105.
current,maximum and minimum When called, update The operation has been called at least once.

Solution:

  • Obviously, for time stamps and prices, we store them in kv and use a variable to maintain the maximum time stamp while traversing. However, for such storage, we also need an ordered set to store the maximum and minimum values of stock prices. Therefore, we can use two sets to store them respectively, and use the ordered feature of TreeMap to complete it at the same time.

Code:

class StockPrice {
    int cur;
    Map<Integer, Integer> map;
    TreeMap<Integer, Integer> tmap;

    public StockPrice(){
        cur = 0;
        map = new HashMap<>();
        tmap = new TreeMap<>();
    }

    public void update(int timestamp, int price) {
        cur = Math.max(cur, timestamp);
        if (map.containsKey(timestamp)) {
            int old = map.get(timestamp);
            int cnt = tmap.get(old);
            if (cnt == 1) {
                tmap.remove(old);
            }
            else {
                tmap.put(old, cnt - 1);
            }
        }
        map.put(timestamp, price);
        tmap.put(price, tmap.getOrDefault(price, 0) + 1);
    }
    
    public int current() {
        return map.get(cur);
    }
    
    public int maximum() {
        return tmap.lastKey();
    }
    
    public int minimum() {
        return tmap.firstKey();
    }
}
/**
 * Your StockPrice object will be instantiated and called as such:
 * StockPrice obj = new StockPrice();
 * obj.update(timestamp,price);
 * int param_2 = obj.current();
 * int param_3 = obj.maximum();
 * int param_4 = obj.minimum();
 */

Topics: Algorithm leetcode