simple AC cpp solution using queue<int>


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

Log in to reply
 

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