My simple c++ solution deque


  • 0
    K
    class HitCounter {
    deque<int>count;
    public:
    /** Initialize your data structure here. */
    HitCounter() {
        
    }
    
    /** Record a hit.
        @param timestamp - The current timestamp (in seconds granularity). */
    void hit(int timestamp) {
        int interval=timestamp - count.front();
        int pop=count.front();
        
            while(interval >=300 &&!count.empty()){
                interval= (count.front()==pop) ? interval: timestamp - count.front();
               if(interval < 300)break;
                pop=count.front();
               cout << pop << " " << timestamp << endl;
                count.pop_front();
            }
            count.push_back(timestamp);
    }
    
    /** Return the number of hits in the past 5 minutes.
        @param timestamp - The current timestamp (in seconds granularity). */
    int getHits(int timestamp) {
        int interval=timestamp - count.front();
        int pop=count.front();
        if(interval < 300)return count.size();
        else {
            while(interval >=300 &&!count.empty()){
                interval= (count.front()==pop) ? interval: timestamp - count.front();
                if(interval < 300)break;
                pop=count.front();
                count.pop_front();
            }
            return count.size();
        }
    }
    

    };


Log in to reply
 

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