最近请求次数
# 📃 题目描述
题目链接:
- 剑指 Offer II 042. 最近请求次数 - 力扣(LeetCode) (leetcode-cn.com) (opens new window)
- 933. 最近的请求次数 - 力扣(LeetCode) (leetcode-cn.com) (opens new window)
# 🔔 解题思路
和上一题 剑指 Offer II 041. 滑动窗口的平均值 - 力扣(LeetCode) (leetcode-cn.com) (opens new window) 思路差不多,甚至更简单点了,因为不需要计算窗口中的和了
class RecentCounter {
private Queue<Integer> queue;
public RecentCounter() {
this.queue = new LinkedList<>();
}
public int ping(int t) {
queue.offer(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);
*/
# 💥 复杂度分析
- 空间复杂度:O(N)
- 时间复杂度:O(1)
🎁 公众号

各位小伙伴大家好呀,叫我小牛肉就行,目前在读东南大学硕士,上方扫码关注公众号「飞天小牛肉」,与你分享我的成长历程与技术感悟~
帮助小牛肉改善此页面 (opens new window)
Last Updated: 2023/02/16, 11:27:10