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();
*/
01-23
447
05-31