力扣2034.股票价格波动
-
懒删除堆
- 见2349.
- 本题从一个优先队列 变为两个优先队列
-
class StockPrice { using PII = pair<int,int>; priority_queue<PII,vector<PII>,less<PII>> big_stocks; priority_queue<PII,vector<PII>,greater<PII>> small_stocks; PII cur_stock{0,0}; unordered_map<int,int> records; public: template <typename T> void update_pq(const PII &stock,T &pq) { pq.push(stock); while(records[pq.top().second] != pq.top().first) pq.pop(); } void update(int timestamp, int price) { PII update_stock = {price,timestamp}; records[timestamp] = price; if(cur_stock.second <= timestamp) cur_stock = update_stock; update_pq(update_stock,big_stocks); update_pq(update_stock,small_stocks); } int current() { return cur_stock.first; } int maximum() { return big_stocks.top().first; } int minimum() { return small_stocks.top().first; } };