fromsortedcontainersimportSortedListclassStockPrice:def__init__(self):self.d={}self.ls=SortedList()self.last=0defupdate(self,timestamp:int,price:int)->None:iftimestampinself.d:self.ls.remove(self.d[timestamp])self.d[timestamp]=priceself.ls.add(price)self.last=max(self.last,timestamp)defcurrent(self)->int:returnself.d[self.last]defmaximum(self)->int:returnself.ls[-1]defminimum(self)->int:returnself.ls[0]# Your StockPrice object will be instantiated and called as such:# obj = StockPrice()# obj.update(timestamp,price)# param_2 = obj.current()# param_3 = obj.maximum()# param_4 = obj.minimum()
classStockPrice{privateMap<Integer,Integer>d=newHashMap<>();privateTreeMap<Integer,Integer>ls=newTreeMap<>();privateintlast;publicStockPrice(){}publicvoidupdate(inttimestamp,intprice){if(d.containsKey(timestamp)){intold=d.get(timestamp);if(ls.merge(old,-1,Integer::sum)==0){ls.remove(old);}}d.put(timestamp,price);ls.merge(price,1,Integer::sum);last=Math.max(last,timestamp);}publicintcurrent(){returnd.get(last);}publicintmaximum(){returnls.lastKey();}publicintminimum(){returnls.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(); */
classStockPrice{public:StockPrice(){}voidupdate(inttimestamp,intprice){if(d.count(timestamp)){ls.erase(ls.find(d[timestamp]));}d[timestamp]=price;ls.insert(price);last=max(last,timestamp);}intcurrent(){returnd[last];}intmaximum(){return*ls.rbegin();}intminimum(){return*ls.begin();}private:unordered_map<int,int>d;multiset<int>ls;intlast=0;};/** * 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(); */