Very concise Java queue solution. Is this a easy question?


  • 0
    X
    public class HitCounter {
    
        Queue<Integer> queue;
        /** Initialize your data structure here. */
        public HitCounter() {
            queue = new LinkedList<>();
        }
        
        /** Record a hit.
            @param timestamp - The current timestamp (in seconds granularity). */
        public void hit(int timestamp) {
            queue.offer(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(!queue.isEmpty()&&timestamp - queue.peek() >= 300){
                queue.poll();
            }
            return queue.size();
        }
    }
    

Log in to reply
 

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