重视树形图理解问题

发布于 2025-01-12 22:11:24 字数 1775 浏览 0 评论 0原文

我当时正在解决一道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 great

  1.  tm.put(oldprice, tm.get(oldprice)-1); 
    
  2.  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

  1.                 tm.put(oldprice, tm.get(oldprice)-1); 
    
  2.         tm.put (price, tm.getOrDefault(price,0)+1);
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁吢 2025-01-19 22:11:24

要解决这个问题,您需要知道股票以特定价格交易的频率。

举个例子:

  • 在时间戳 1 交易 10,在时间戳 2 交易 5。现在最高价格是 10。(一笔交易 10,一笔交易 5)
  • 时间戳 1 的价格更新为 3。现在最高价格为 5。(一笔交易为 3,一笔交易为 5)

另一个例子:

  • 在时间戳 1 交易 10,在时间戳 2 交易 5,在时间戳 3 交易 10。最高价格也是 10。(有两笔交易 10,一笔交易 5)
  • 时间戳 1 的价格更新为 3。现在最高价格仍然是 10(因为时间戳 3 的交易)。 (一笔交易为 10 个,一笔交易为 3 个,一笔交易为 5 个)

值得注意的是

    tm.put (price, tm.getOrDefault(price,0)+1);

,针对特定价格又发生了一笔交易。

当旧交易更新时,

    if (tm.get(oldprice)==1) {
        tm.remove(oldprice); // 
    } else {
        tm.put(oldprice, tm.get(oldprice)-1); 
    }

要么删除旧价格的条目(如果该价格只有一笔交易),要么指出该价格少一笔交易。

To solve the problem you need to know how often the stock is traded for a particular price.

An example:

  • it was traded at timestamp 1 for 10, at timestamp 2 for 5. Now the maximum price is 10. (There was one trade for 10, one trade for 5)
  • the price for timestamp 1 is updated to 3. Now the maximum price is 5. (There was one trade for 3, one trade for 5)

Another example:

  • it was traded at timestamp 1 for 10, at timestamp 2 for 5, at timestamp 3 for 10. The maximum price is also 10. (There were two trades for 10, one trade for 5)
  • the price for timestamp 1 is updated to 3. Now the maximum price is still 10 (because of the trade at timestamp 3). (There was one trade for 10, one trade for 3, one trade for 5)

With

    tm.put (price, tm.getOrDefault(price,0)+1);

it is noted that one more trade occurred for a specific price.

When an older trade is updated,

    if (tm.get(oldprice)==1) {
        tm.remove(oldprice); // 
    } else {
        tm.put(oldprice, tm.get(oldprice)-1); 
    }

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文