Loading
Leetcode / 933. Number of Recent Calls

Pick a programming language:

Here is the source code for the solution to this problem.

import java.util.LinkedList;
import java.util.Queue;

class RecentCounter {
    private Queue<Integer> queue;

    public RecentCounter() {
        queue = new LinkedList<Integer>();
    }
    
    public int ping(int t) {
        queue.offer(t);
        int lowerBound = t - 3000;
        while (queue.peek() < lowerBound) {
            queue.poll();
        }
        return queue.size();
    }
}

/**
 * Your RecentCounter object will be instantiated and called as such:
 * RecentCounter obj = new RecentCounter();
 * int param_1 = obj.ping(t);
 */
Did you like the lesson? 😆👍
Consider a donation to support our work: