[코테 풀이] Number of Recent Calls

시내·2024년 6월 29일

Q_933) Number of Recent Calls

출처 : https://leetcode.com/problems/number-of-recent-calls/

You have a RecentCounter class which counts the number of recent requests within a certain time frame.

Implement the RecentCounter class:

  • RecentCounter() Initializes the counter with zero recent requests.
  • int ping(int t) Adds a new request at time t, where t represents some time in milliseconds, and returns the number of requests that has happened in the past 3000 milliseconds (including the new request). Specifically, return the number of requests that have happened in the inclusive range[t - 3000, t].

It is guaranteed that every call to ping uses a strictly larger value of t than the previous call.

class RecentCounter {
    ArrayList<Integer> requests;
    
    public RecentCounter() {
        requests = new ArrayList<>();        
    }
    
    public int ping(int t) {
        int count = 0;
        requests.add(t);
        if(requests.size()==1) return 1;
        for(int i = 0; i < requests.size(); i++){
            if(requests.get(i) >= t-3000 && requests.get(i) <= t){
                count++;
            }
        }
        return count;
    }
}

/**
 * Your RecentCounter object will be instantiated and called as such:
 * RecentCounter obj = new RecentCounter();
 * int param_1 = obj.ping(t);
 */
profile
contact 📨 ksw08215@gmail.com

0개의 댓글