【力扣-每日一题】2034. 股票价格波动

【力扣-每日一题】2034. 股票价格波动_第1张图片

class StockPrice {
private:
    unordered_map<int,int> mp;  //存储日期及其对应的价格
    multiset<int> st;   //存储所有价格
    int last_day;   //最新一天
public:
    StockPrice() {
        this->last_day=0;
    }
    
    void update(int timestamp, int price) {
        if(mp.find(timestamp)!=mp.end())//如果能找到
             //删除再添加
             st.erase(st.find(mp[timestamp]));
        mp[timestamp]=price;    //更新
        st.insert(price);   
        this->last_day=this->last_day>timestamp?this->last_day:timestamp;//日期更新
            
    }
    
    int current() {
        return mp[this->last_day];
    }
    
    int maximum() {
        return *st.rbegin();//返回最大值
    }
    
    int minimum() {
        return *st.begin();//返回最小值
    }
};

/**
 * 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();
 */

你可能感兴趣的:(练题杂记,leetcode)