Loading

LeetCode problem#933

Number of Recent Calls

Solved in 1 languageJava

Solve it on LeetCode(opens in a new tab)

Solution

25 lines
1import java.util.LinkedList;
2import java.util.Queue;
3
4class RecentCounter {
5    private Queue<Integer> queue;
6
7    public RecentCounter() {
8        queue = new LinkedList<Integer>();
9    }
10    
11    public int ping(int t) {
12        queue.offer(t);
13        int lowerBound = t - 3000;
14        while (queue.peek() < lowerBound) {
15            queue.poll();
16        }
17        return queue.size();
18    }
19}
20
21/**
22 * Your RecentCounter object will be instantiated and called as such:
23 * RecentCounter obj = new RecentCounter();
24 * int param_1 = obj.ping(t);
25 */

Want a walkthrough of this Java solution?

Get a step-by-step explanation of the idea behind the code.

All problems

Problem statements belong to LeetCode and are on leetcode.com. We're not affiliated with LeetCode.