Leetcode 933 Number of Recent Calls

1517 단어 JavaAlgorithmLeetcode
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.
 
Example 1:
Input: inputs = ["RecentCounter","ping","ping","ping","ping"], inputs = [[],[1],[100],[3001],[3002]]
Output: [null,1,2,3,3]

이 문 제 는 주어진 숫자 에서 조건 에 맞 는 부분 을 찾 는 것 이다.
1)
class RecentCounter {
    PriorityQueue queue = new PriorityQueue<>();
    public RecentCounter() {
        
    }
    
    public int ping(int t) {
        queue.add(t);
        while(queue.peek() < t - 3000){
            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);
 */

2)
class RecentCounter {
    set history;
public:
    int ping(int t) {
        history.insert( t );
        return distance( history.lower_bound( t - 3000 ), history.end() );
    }
};

좋은 웹페이지 즐겨찾기