我正在处理一个密码问题。并在讨论部分找到了解决方案。
问题- https://leetcode.com/problems/stock-price-fluctuation/
解决方案-
class StockPrice {
HashMap<Integer, Integer> hm; //timestamp,price
TreeMap<Integer, Integer> tm; //price, frequency
int current;
public StockPrice() {
hm = new HashMap<>();
tm = new TreeMap<>();
current = 0;
}
public void update(int timestamp, int price) {
//check whether latest timestamp or current timestamp is larger...
current = Math.max(current, timestamp); //if timesatamp already present
if(hm.containsKey(timestamp))
{
int oldprice=hm.get(timestamp);
if(tm.get(oldprice)==1){
tm.remove(oldprice); //
}
else{
tm.put(oldprice, tm.get(oldprice)-1);
}
}
//update new price in hm
hm.put(timestamp, price);
//update new frequency of new price in treemap
tm.put (price, tm.getOrDefault(price,0)+1);
}
public int current() {
return hm.get(current);
}
public int maximum() {
return tm.lastKey();
}
public int minimum() {
return tm.firstKey();
}
}但我不明白以下几点。如果有人能解释这将是很棒的
发布于 2022-03-10 08:36:12
为了解决这个问题,你需要知道股票以特定价格交易的频率。
举个例子:
。
另一个例子是:
)
使用
tm.put (price, tm.getOrDefault(price,0)+1);值得注意的是,在一个特定的价格下还发生了一次贸易。
当旧的交易被更新时,
if (tm.get(oldprice)==1) {
tm.remove(oldprice); //
} else {
tm.put(oldprice, tm.get(oldprice)-1);
}要么删除旧价格的条目(如果该价格只有一个交易),要么注意到该价格有一个交易低于该价格。
https://stackoverflow.com/questions/71412352
复制相似问题