C++_Queue_0ms_Accepted


  • 0

    First-in-first-out when the time range is larger than 300.

    class HitCounter {
    queue<int> Q;
    public:
    /** Initialize your data structure here. */
    HitCounter() {
        
    }
    
    /** Record a hit.
        @param timestamp - The current timestamp (in seconds granularity). */
    void hit(int timestamp) {
        Q.push(timestamp);
    }
    
    /** Return the number of hits in the past 5 minutes.
        @param timestamp - The current timestamp (in seconds granularity). */
    int getHits(int timestamp) {
        if(timestamp - Q.front() < 300){
            return Q.size();
        }else{
            while(!Q.empty() && timestamp - Q.front() >= 300){
                Q.pop();
            }
            return Q.size();
        }
    }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.