ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

[Google] LeetCode 2034 Stock Price Fluctuation

2022-08-20 22:32:03  阅读:187  来源: 互联网

标签:Google int Fluctuation Price top timestamp push price stock


You are given a stream of records about a particular stock. Each record contains a timestamp and the corresponding price of the stock at that timestamp.

Unfortunately due to the volatile nature of the stock market, the records do not come in order. Even worse, some records may be incorrect. Another record with the same timestamp may appear later in the stream correcting the price of the previous wrong record.

Design an algorithm that:

  • Updates the price of the stock at a particular timestamp, correcting the price from any previous records at the timestamp.
  • Finds the latest price of the stock based on the current records. The latest price is the price at the latest timestamp recorded.
  • Finds the maximum price the stock has been based on the current records.
  • Finds the minimum price the stock has been based on the current records.

Implement the StockPrice class:

  • StockPrice() Initializes the object with no price records.
  • void update(int timestamp, int price) Updates the price of the stock at the given timestamp.
  • int current() Returns the latest price of the stock.
  • int maximum() Returns the maximum price of the stock.
  • int minimum() Returns the minimum price of the stock.

Solution

构造一个大根堆和小根堆,这里可以用 \(priority\_queue\) 来实现。但是我们还需要删除队列中的任何一个元素,这里我们额外使用一个相同的队列,将需要删除的元素 \(push\) 进去。求 \(top\) 的时候只需要利用 \(while\) 来 \(pop\) 出相同的队首元素即可。

点击查看代码
class StockPrice {
private:
    map<int,int> record;
    priority_queue<int> ts;
    
    typedef struct _MAXHEAP {
        priority_queue<int> Q,D;
        void push(int x) {Q.push(x);}
        void erase(int x) {D.push(x);}
        bool empty() {
            return Q.empty();
        }
        int top() {
            while(!D.empty()&&Q.top()==D.top()) {Q.pop();D.pop();}
            return Q.empty()?0:Q.top();
        }
    } MAXHEAP;
    
    typedef struct _MINHEAP {
        priority_queue<int, vector<int>, greater<int> > Q,D;
        void push(int x) {Q.push(x);}
        void erase(int x) {D.push(x);}
        bool empty() {
            return Q.empty();
        }
        int top() {
            while(!D.empty()&&Q.top()==D.top()) {Q.pop();D.pop();}
            return Q.empty()?0:Q.top();
        }
    } MINHEAP;
    
    MAXHEAP maxQ;
    MINHEAP minQ;
    
public:
    StockPrice() {
        
    }
    
    void update(int timestamp, int price) {
        if(!record[timestamp]){
            record[timestamp] = price;
            maxQ.push(price);
            minQ.push(price);
            ts.push(timestamp);
        }
        else{
            int prev = record[timestamp];
            maxQ.erase(prev); minQ.erase(prev);
            record[timestamp] = price;
            maxQ.push(price); minQ.push(price);
        }
        
    }
    
    int current() {
        return record[ts.top()];
    }
    
    int maximum() {
        return maxQ.top();
    }
    
    int minimum() {
        return minQ.top();
    }
};

/**
 * 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();
 */

标签:Google,int,Fluctuation,Price,top,timestamp,push,price,stock
来源: https://www.cnblogs.com/xinyu04/p/16608921.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有