由于一定是按照时间顺序,只需要维护一个队列,其中队头元素大于t-3000即可。
class RecentCounter {
private:
queue<int> q;
public:
RecentCounter() {
}
int ping(int t) {
while(!q.empty() && t - q.front() > 3000) {
q.pop();
}
q.push(t);
return q.size();
}
};
/**
* Your RecentCounter object will be instantiated and called as such:
* RecentCounter* obj = new RecentCounter();
* int param_1 = obj->ping(t);
*/