# 0362. Design Hit Counter ###### tags: `Leetcode` `Medium` `Queue` Link: https://leetcode.com/problems/design-hit-counter/ ## 思路 time complexity: $hit:O(1) getHits:O(N)$ amortized $O(1)$ space complexity: $O(N)$ 利用queue first in first out的性质 (因为calls are being made to the system in chronological order (i.e., timestamp is monotonically increasing)) ## Code ```java= class HitCounter { private Queue<Integer> hits; public HitCounter() { hits = new LinkedList<>(); } public void hit(int timestamp) { hits.add(timestamp); } public int getHits(int timestamp) { while(!hits.isEmpty()){ int diff = timestamp-hits.peek(); if(diff>=300) hits.remove(); else break; } return hits.size(); } } ```