重视树形图理解问题
我当时正在解决一道leetcode问题。并在讨论部分
问题中找到了解决方案 - https://leetcode.com/problems/stock -价格-波动/
解决方案-
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();
}
}
but I do not understand the following parts. if someone could explain that would be greattm.put(oldprice, tm.get(oldprice)-1);
tm.put(价格, tm.getOrDefault(价格,0)+1);
I was running through a leetcode problem. and found a solution in the discussion section
problem- https://leetcode.com/problems/stock-price-fluctuation/
solution-
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();
}
}
but I do not understand the following parts.
if someone could explain that would be great
tm.put(oldprice, tm.get(oldprice)-1);
tm.put (price, tm.getOrDefault(price,0)+1);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要解决这个问题,您需要知道股票以特定价格交易的频率。
举个例子:
另一个例子:
值得注意的是
,针对特定价格又发生了一笔交易。
当旧交易更新时,
要么删除旧价格的条目(如果该价格只有一笔交易),要么指出该价格少一笔交易。
To solve the problem you need to know how often the stock is traded for a particular price.
An example:
Another example:
With
it is noted that one more trade occurred for a specific price.
When an older trade is updated,
either removes the entry for the old price (if there was only one trade for that price) or notes that there was one trade less for that price.