Simple solution using deque C++


  • 0
    D
    #include <deque>;
    class HitCounter {
    private:
        int count = 0;
        deque<int> q;
    public:
        HitCounter() {
            
        }
        void hit(int t) {
            q.push_back(t);
        }
        
        int getHits(int t) {
            while(q.size()> 0 && q.front() <= t-300){
                q.pop_front();
            }
            return q.size();
        }
    };
    

    This solution assumes getHits will be called often and timestamp will be called in ascending order.


Log in to reply
 

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