-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem_0933_RecentCounter.cc
More file actions
55 lines (49 loc) · 976 Bytes
/
Copy pathProblem_0933_RecentCounter.cc
File metadata and controls
55 lines (49 loc) · 976 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
class RecentCounter
{
private:
// vector<int> arr;
queue<int> que;
public:
RecentCounter() {}
int ping(int t)
{
que.push(t);
while (!que.empty() && que.front() < t - 3000)
{
que.pop();
}
return que.size();
}
// 二分查找
// int ping(int t) {
// arr.push_back(t);
// int idx = -1;
// int n = arr.size();
// int left = 0;
// int right = n - 1;
// int target = t - 3000;
// while(left <= right)
// {
// int mid = (right - left) / 2 + left;
// if(target <= arr[mid])
// {
// idx = mid;
// right = mid - 1;
// }
// else
// {
// left = mid + 1;
// }
// }
// return arr.size() - idx;
// }
};
/**
* Your RecentCounter object will be instantiated and called as such:
* RecentCounter* obj = new RecentCounter();
* int param_1 = obj->ping(t);
*/