import java.util.LinkedList; import java.util.Queue; class HitCounter { private Queue<Integer> q = new LinkedList<Integer>(); /** Initialize your data structure here. */ HitCounter() {} /** Record a hit. @param timestamp - The current timestamp (in seconds granularity). */ void hit(int timestamp) { q.offer(timestamp); } /** Return the number of hits in the past 5 minutes. @param timestamp - The current timestamp (in seconds granularity). */ int getHits(int timestamp) { while (!q.isEmpty() && timestamp - q.peek() >= 300) { q.poll(); } return q.size(); } }