-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ933RecentCalls.java
More file actions
30 lines (25 loc) · 849 Bytes
/
Q933RecentCalls.java
File metadata and controls
30 lines (25 loc) · 849 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
/*
@b-knd (jingru) on 05 August 2022 08:48:00
*/
class RecentCounter {
Queue<Integer> queue;
//initiate a queue to keep track of pings
public RecentCounter() {
queue = new LinkedList<>();
}
//remove all pings more than 3000 milliseconds before t using while loop and return queue size
public int ping(int t) {
queue.add(t);
while(queue.size()>0 && queue.peek() < t-3000){
queue.remove();
}
return queue.size();
}
}
//Runtime: 33 ms, faster than 65.74% of Java online submissions for Number of Recent Calls.
//Memory Usage: 68.3 MB, less than 74.30% of Java online submissions for Number of Recent Calls.
/**
* Your RecentCounter object will be instantiated and called as such:
* RecentCounter obj = new RecentCounter();
* int param_1 = obj.ping(t);
*/