classHitCounter:def__init__(self):self.ts=[]defhit(self,timestamp:int)->None:self.ts.append(timestamp)defgetHits(self,timestamp:int)->int:returnlen(self.ts)-bisect_left(self.ts,timestamp-300+1)# Your HitCounter object will be instantiated and called as such:# obj = HitCounter()# obj.hit(timestamp)# param_2 = obj.getHits(timestamp)
classHitCounter{privateList<Integer>ts=newArrayList<>();publicHitCounter(){}publicvoidhit(inttimestamp){ts.add(timestamp);}publicintgetHits(inttimestamp){intl=search(timestamp-300+1);returnts.size()-l;}privateintsearch(intx){intl=0,r=ts.size();while(l<r){intmid=(l+r)>>1;if(ts.get(mid)>=x){r=mid;}else{l=mid+1;}}returnl;}}/** * Your HitCounter object will be instantiated and called as such: * HitCounter obj = new HitCounter(); * obj.hit(timestamp); * int param_2 = obj.getHits(timestamp); */
1 2 3 4 5 6 7 8 9101112131415161718192021222324
classHitCounter{public:HitCounter(){}voidhit(inttimestamp){ts.push_back(timestamp);}intgetHits(inttimestamp){returnts.end()-lower_bound(ts.begin(),ts.end(),timestamp-300+1);}private:vector<int>ts;};/** * Your HitCounter object will be instantiated and called as such: * HitCounter* obj = new HitCounter(); * obj->hit(timestamp); * int param_2 = obj->getHits(timestamp); */
1 2 3 4 5 6 7 8 910111213141516171819202122
typeHitCounterstruct{ts[]int}funcConstructor()HitCounter{returnHitCounter{}}func(this*HitCounter)Hit(timestampint){this.ts=append(this.ts,timestamp)}func(this*HitCounter)GetHits(timestampint)int{returnlen(this.ts)-sort.SearchInts(this.ts,timestamp-300+1)}/** * Your HitCounter object will be instantiated and called as such: * obj := Constructor(); * obj.Hit(timestamp); * param_2 := obj.GetHits(timestamp); */
classHitCounter{privatets:number[]=[];constructor(){}hit(timestamp:number):void{this.ts.push(timestamp);}getHits(timestamp:number):number{constsearch=(x:number)=>{let[l,r]=[0,this.ts.length];while(l<r){constmid=(l+r)>>1;if(this.ts[mid]>=x){r=mid;}else{l=mid+1;}}returnl;};returnthis.ts.length-search(timestamp-300+1);}}/** * Your HitCounter object will be instantiated and called as such: * var obj = new HitCounter() * obj.hit(timestamp) * var param_2 = obj.getHits(timestamp) */
structHitCounter{ts:Vec<i32>,}/** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */implHitCounter{fnnew()->Self{HitCounter{ts:Vec::new()}}fnhit(&mutself,timestamp:i32){self.ts.push(timestamp);}fnget_hits(&self,timestamp:i32)->i32{letl=self.search(timestamp-300+1);(self.ts.len()-l)asi32}fnsearch(&self,x:i32)->usize{let(mutl,mutr)=(0,self.ts.len());whilel<r{letmid=(l+r)/2;ifself.ts[mid]>=x{r=mid;}else{l=mid+1;}}l}}