Number of Recent Calls
让设计一个数据结构, 给一个方法ping, 求3000ms内ping的数量.
因为只问数量, 用pq直接存数字可以求解.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
class RecentCounter { PriorityQueue<Integer> pq = new PriorityQueue<>(); public RecentCounter() { } public int ping(int t) { pq.add(t); while(pq.peek() < t - 3000){ pq.poll(); } return pq.size(); } } /** * Your RecentCounter object will be instantiated and called as such: * RecentCounter obj = new RecentCounter(); * int param_1 = obj.ping(t); */ |