-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecentCounter.cpp
More file actions
32 lines (28 loc) · 736 Bytes
/
RecentCounter.cpp
File metadata and controls
32 lines (28 loc) · 736 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class RecentCounter {
public:
vector<int> requests;
RecentCounter() {
this->requests = {};
}
int ping(int t) {
this->requests.push_back(t);
int hi = this->requests.size() - 1;
int lo = 0;
int mid;
int lowerLimit = t - 3000;
while(lo < hi) {
mid = lo + (hi - lo) / 2;
if(this->requests[mid] < lowerLimit) {
lo = mid + 1;
} else {
hi = mid;
}
}
return this->requests.size() - lo;
}
};
/**
* Your RecentCounter object will be instantiated and called as such:
* RecentCounter* obj = new RecentCounter();
* int param_1 = obj->ping(t);
*/