AC one queue c++


  • 0

    Not the fastest, but the most straight forward

    class HitCounter {
    private:
        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) {
            while (q.size() && 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.