# 2034. Stock Price Fluctuation ###### tags: `Leetcode` `Medium` Link: https://leetcode.com/problems/stock-price-fluctuation/ ## 思路 注意update不是按照时间顺序给的,所以必须要记录下来所有之前的price record map: key:timestamp, value:price vals map: key: price, value: how many timestamp has $price$ as the current price ## Code ```java= class StockPrice { Map<Integer, Integer> record; TreeMap<Integer, Integer> vals; int lastest = 0; public StockPrice() { record = new HashMap<>(); vals = new TreeMap<>(); } public void update(int timestamp, int price) { lastest = Math.max(lastest, timestamp); if(record.containsKey(timestamp)){ int prevPrice = record.get(timestamp); vals.put(prevPrice, vals.get(prevPrice)-1); if(vals.get(prevPrice)==0) vals.remove(prevPrice); } record.put(timestamp, price); vals.put(price, vals.getOrDefault(price, 0)+1); } public int current() { return record.get(lastest); } public int maximum() { return vals.lastKey(); } public int minimum() { return vals.firstKey(); } } ```