C++ simple queue 5 lines


  • 0
    B

    nothing special

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

Log in to reply
 

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