https://leetcode.com/problems/number-of-recent-calls/
https://leetcode.com/problems/number-of-recent-calls/discuss/189239/Java-Three-solutions%3A-TreeMap-TreeSet-ArrayList-All-time-O(logN).
Write a class
RecentCounter
to count recent requests.
It has only one method:
ping(int t)
, where t represents some time in milliseconds.
Return the number of
ping
s that have been made from 3000 milliseconds ago until now.
Any ping with time in
[t - 3000, t]
will count, including the current ping.
It is guaranteed that every call to
ping
uses a strictly larger value of t
than before.
Time:
O(N)
, space: O(Math.min(N, 3000))
.
class RecentCounter {
Queue<Integer> q;
public RecentCounter() {
q = new LinkedList();
}
public int ping(int t) {
q.add(t);
while (q.peek() < t - 3000)
q.poll();
return q.size();
}
}
https://leetcode.com/problems/number-of-recent-calls/discuss/189239/Java-Three-solutions%3A-TreeMap-TreeSet-ArrayList-All-time-O(logN).