classHitCounter:def__init__(self):""" Initialize your data structure here. """self.counter=Counter()defhit(self,timestamp:int)->None:""" Record a hit. @param timestamp - The current timestamp (in seconds granularity). """self.counter[timestamp]+=1defgetHits(self,timestamp:int)->int:""" Return the number of hits in the past 5 minutes. @param timestamp - The current timestamp (in seconds granularity). """returnsum([vfort,vinself.counter.items()ift+300>timestamp])# Your HitCounter object will be instantiated and called as such:# obj = HitCounter()# obj.hit(timestamp)# param_2 = obj.getHits(timestamp)
classHitCounter{privateMap<Integer,Integer>counter;/** Initialize your data structure here. */publicHitCounter(){counter=newHashMap<>();}/** Record a hit. @param timestamp - The current timestamp (in seconds granularity). */publicvoidhit(inttimestamp){counter.put(timestamp,counter.getOrDefault(timestamp,0)+1);}/** Return the number of hits in the past 5 minutes. @param timestamp - The current timestamp (in seconds granularity). */publicintgetHits(inttimestamp){inthits=0;for(Map.Entry<Integer,Integer>entry:counter.entrySet()){if(entry.getKey()+300>timestamp){hits+=entry.getValue();}}returnhits;}}/** * Your HitCounter object will be instantiated and called as such: * HitCounter obj = new HitCounter(); * obj.hit(timestamp); * int param_2 = obj.getHits(timestamp); */
usestd::{collections::BinaryHeap,cmp::Reverse};structHitCounter{/// A min heappq:BinaryHeap<Reverse<i32>>,}implHitCounter{fnnew()->Self{Self{pq:BinaryHeap::new(),}}fnhit(&mutself,timestamp:i32){self.pq.push(Reverse(timestamp));}fnget_hits(&mutself,timestamp:i32)->i32{whileletSome(Reverse(min_elem))=self.pq.peek(){if*min_elem<=timestamp-300{self.pq.pop();}else{break;}}self.pq.len()asi32}}