Beats 97% of Solution using Simple Queue


  • 1

    public class HitCounter {

    /** Initialize your data structure here. */
      Queue <Integer> pq;
    public HitCounter() {
        
       pq= new LinkedList <Integer>();
        
    }
    
    /** Record a hit.
        @param timestamp - The current timestamp (in seconds granularity). */
    public void hit(int timestamp) {
      
          
       while (pq.size()>0 && timestamp-pq.peek()>=300)
       pq.poll();
    
      pq.add(timestamp);
    }
    
    /** Return the number of hits in the past 5 minutes.
        @param timestamp - The current timestamp (in seconds granularity). */
    public int getHits(int timestamp) {
        
         while (pq.size()>0 && timestamp-pq.peek()>=300)
         pq.poll();
    
        return pq.size();
        
    }
    

    }


Log in to reply
 

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