You have a RecentCounter class which counts the number of recent requests within a certain time frame.
Implement the RecentCounter class:
RecentCounter() Initializes the counter with zero recent requests.
int ping(int t) Adds a new request at time t, where t represents some time in milliseconds, and returns the number of requests that has happened in the past 3000 milliseconds (including the new request). Specifically, return the number of requests that have happened in the inclusive range [t - 3000, t].
It is guaranteed that every call to ping uses a strictly larger value of t than the previous call.
Example 1:
Input
["RecentCounter", "ping", "ping", "ping", "ping"]
[[], [1], [100], [3001], [3002]]
Output
[null, 1, 2, 3, 3]
Explanation
RecentCounter recentCounter = new RecentCounter();
recentCounter.ping(1); // requests = [1], range is [-2999,1], return 1
recentCounter.ping(100); // requests = [1, 100], range is [-2900,100], return 2
recentCounter.ping(3001); // requests = [1, 100, 3001], range is [1,3001], return 3
recentCounter.ping(3002); // requests = [1, 100, 3001, 3002], range is [2,3002], return 3
Constraints:
1 <= t <= 109
Each test case will call ping with strictly increasing values of t.
At most 104 calls will be made to ping.
Solutions
Solution 1
1 2 3 4 5 6 7 8 91011121314
classRecentCounter:def__init__(self):self.q=deque()defping(self,t:int)->int:self.q.append(t)whileself.q[0]<t-3000:self.q.popleft()returnlen(self.q)# Your RecentCounter object will be instantiated and called as such:# obj = RecentCounter()# param_1 = obj.ping(t)
classRecentCounter{privateint[]s=newint[10010];privateintidx;publicRecentCounter(){}publicintping(intt){s[idx++]=t;returnidx-search(t-3000);}privateintsearch(intx){intleft=0,right=idx;while(left<right){intmid=(left+right)>>1;if(s[mid]>=x){right=mid;}else{left=mid+1;}}returnleft;}}/** * Your RecentCounter object will be instantiated and called as such: * RecentCounter obj = new RecentCounter(); * int param_1 = obj.ping(t); */
1 2 3 4 5 6 7 8 910111213141516171819
classRecentCounter{public:queue<int>q;RecentCounter(){}intping(intt){q.push(t);while(q.front()<t-3000)q.pop();returnq.size();}};/** * Your RecentCounter object will be instantiated and called as such: * RecentCounter* obj = new RecentCounter(); * int param_1 = obj->ping(t); */
1 2 3 4 5 6 7 8 9101112131415161718192021
typeRecentCounterstruct{q[]int}funcConstructor()RecentCounter{returnRecentCounter{[]int{}}}func(this*RecentCounter)Ping(tint)int{this.q=append(this.q,t)forthis.q[0]<t-3000{this.q=this.q[1:]}returnlen(this.q)}/** * Your RecentCounter object will be instantiated and called as such: * obj := Constructor(); * param_1 := obj.Ping(t); */
1 2 3 4 5 6 7 8 9101112131415161718192021
classRecentCounter{privatequeue:number[];constructor(){this.queue=[];}ping(t:number):number{this.queue.push(t);while(this.queue[0]<t-3000){this.queue.shift();}returnthis.queue.length;}}/** * Your RecentCounter object will be instantiated and called as such: * var obj = new RecentCounter() * var param_1 = obj.ping(t) */
usestd::collections::VecDeque;structRecentCounter{queue:VecDeque<i32>,}/** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */implRecentCounter{fnnew()->Self{Self{queue:VecDeque::new(),}}fnping(&mutself,t:i32)->i32{self.queue.push_back(t);whileletSome(&v)=self.queue.front(){ifv>=t-3000{break;}self.queue.pop_front();}self.queue.len()asi32}}
1 2 3 4 5 6 7 8 9101112131415161718192021
varRecentCounter=function(){this.q=[];};/** * @param {number} t * @return {number} */RecentCounter.prototype.ping=function(t){this.q.push(t);while(this.q[0]<t-3000){this.q.shift();}returnthis.q.length;};/** * Your RecentCounter object will be instantiated and called as such: * var obj = new RecentCounter() * var param_1 = obj.ping(t) */
1 2 3 4 5 6 7 8 910111213141516171819202122
publicclassRecentCounter{privateQueue<int>q=newQueue<int>();publicRecentCounter(){}publicintPing(intt){q.Enqueue(t);while(q.Peek()<t-3000){q.Dequeue();}returnq.Count;}}/** * Your RecentCounter object will be instantiated and called as such: * RecentCounter obj = new RecentCounter(); * int param_1 = obj.Ping(t); */
Solution 2
1 2 3 4 5 6 7 8 9101112
classRecentCounter:def__init__(self):self.s=[]defping(self,t:int)->int:self.s.append(t)returnlen(self.s)-bisect_left(self.s,t-3000)# Your RecentCounter object will be instantiated and called as such:# obj = RecentCounter()# param_1 = obj.ping(t)
1 2 3 4 5 6 7 8 9101112131415161718
classRecentCounter{public:vector<int>s;RecentCounter(){}intping(intt){s.push_back(t);returns.size()-(lower_bound(s.begin(),s.end(),t-3000)-s.begin());}};/** * Your RecentCounter object will be instantiated and called as such: * RecentCounter* obj = new RecentCounter(); * int param_1 = obj->ping(t); */
typeRecentCounterstruct{s[]int}funcConstructor()RecentCounter{returnRecentCounter{[]int{}}}func(this*RecentCounter)Ping(tint)int{this.s=append(this.s,t)search:=func(xint)int{left,right:=0,len(this.s)forleft<right{mid:=(left+right)>>1ifthis.s[mid]>=x{right=mid}else{left=mid+1}}returnleft}returnlen(this.s)-search(t-3000)}/** * Your RecentCounter object will be instantiated and called as such: * obj := Constructor(); * param_1 := obj.Ping(t); */