当前位置:网站首页>队列题目:最近的请求次数
队列题目:最近的请求次数
2022-08-05 03:11:00 【伟大的车尔尼】
题目
标题和出处
标题:最近的请求次数
出处:933. 最近的请求次数
难度
3 级
题目描述
要求
写一个 RecentCounter \texttt{RecentCounter} RecentCounter 类来计算特定时间范围内最近的请求。
请你实现 RecentCounter \texttt{RecentCounter} RecentCounter 类:
- RecentCounter() \texttt{RecentCounter()} RecentCounter() 初始化计数器,请求数为 0 \texttt{0} 0。
- int ping(int t) \texttt{int ping(int t)} int ping(int t) 在时间 t \texttt{t} t 添加一个新请求,其中 t \texttt{t} t 表示以毫秒为单位的某个时间,并返回过去 3000 \texttt{3000} 3000 毫秒内发生的所有请求数(包括新请求)。确切地说,返回在 [t - 3000, t] \texttt{[t - 3000, t]} [t - 3000, t] 内发生的请求数。
保证每次对 ping \texttt{ping} ping 的调用都使用比之前更大的 t \texttt{t} t 值。
示例
示例 1:
输入:
["RecentCounter", "ping", "ping", "ping", "ping"] \texttt{["RecentCounter", "ping", "ping", "ping", "ping"]} ["RecentCounter", "ping", "ping", "ping", "ping"]
[[], [1], [100], [3001], [3002]] \texttt{[[], [1], [100], [3001], [3002]]} [[], [1], [100], [3001], [3002]]
输出:
[null, 1, 2, 3, 3] \texttt{[null, 1, 2, 3, 3]} [null, 1, 2, 3, 3]
解释:
RecentCounter recentCounter = new RecentCounter(); \texttt{RecentCounter recentCounter = new RecentCounter();} RecentCounter recentCounter = new RecentCounter();
recentCounter.ping(1); \texttt{recentCounter.ping(1);} recentCounter.ping(1); // requests = [1] \texttt{requests = [1]} requests = [1],范围是 [-2999, 1] \texttt{[-2999, 1]} [-2999, 1],返回 1 \texttt{1} 1
recentCounter.ping(100); \texttt{recentCounter.ping(100);} recentCounter.ping(100); // requests = [1, 100] \texttt{requests = [1, 100]} requests = [1, 100],范围是 [-2900, 100] \texttt{[-2900, 100]} [-2900, 100],返回 2 \texttt{2} 2
recentCounter.ping(3001); \texttt{recentCounter.ping(3001);} recentCounter.ping(3001); // requests = [1, 100, 3001] \texttt{requests = [1, 100, 3001]} requests = [1, 100, 3001],范围是 [1, 3001] \texttt{[1, 3001]} [1, 3001],返回 3 \texttt{3} 3
recentCounter.ping(3002); \texttt{recentCounter.ping(3002);} recentCounter.ping(3002); // requests = [1, 100, 3001, 3002] \texttt{requests = [1, 100, 3001, 3002]} requests = [1, 100, 3001, 3002],范围是 [2, 3002] \texttt{[2, 3002]} [2, 3002],返回 3 \texttt{3} 3
数据范围
- 1 ≤ t ≤ 10 9 \texttt{1} \le \texttt{t} \le \texttt{10}^\texttt{9} 1≤t≤109
- 保证每次对 ping \texttt{ping} ping 调用所使用的 t \texttt{t} t 值都严格递增
- 至多调用 ping \texttt{ping} ping 方法 10 4 \texttt{10}^\texttt{4} 104 次
解法
思路和算法
由于每次调用 ping \textit{ping} ping 方法时,请求时间 t t t 是严格单调递增的,因此按照调用顺序存储请求时间可以得到请求时间的严格递增序列。每次调用 ping \textit{ping} ping 方法要求返回过去 3000 3000 3000 毫秒内发生的所有请求数,因此可以将请求时间序列中的距离请求时间超过 3000 3000 3000 毫秒的请求删除,然后计算请求时间序列中的请求数,即为过去 3000 3000 3000 毫秒内发生的所有请求数。
由于最早发生的请求会最先被删除,因此请求时间序列满足先进先出的特点,可以使用队列实现请求时间序列,在构造方法中初始化队列。
对于 ping \textit{ping} ping 方法,首先将过时的请求出队列,如果队列不为空且队首元素距离当前请求时间超过 3000 3000 3000 毫秒,则将队首元素出队列,重复该操作直到队列变为空或者队首元素距离当前请求时间不超过 3000 3000 3000 毫秒。然后将当前请求时间入队列,此时队列内的元素个数即为过去 3000 3000 3000 毫秒内发生的所有请求数,返回队列内的元素个数即可。
代码
class RecentCounter {
static final int RANGE = 3000;
Queue<Integer> queue;
public RecentCounter() {
queue = new ArrayDeque<Integer>();
}
public int ping(int t) {
while (!queue.isEmpty() && queue.peek() < t - RANGE) {
queue.poll();
}
queue.offer(t);
return queue.size();
}
}
复杂度分析
时间复杂度:构造方法的时间复杂度是 O ( 1 ) O(1) O(1),方法 ping \textit{ping} ping 的均摊时间复杂度是 O ( 1 ) O(1) O(1)。每个元素最多入队和出队各一次,因此方法 ping \textit{ping} ping 的均摊时间复杂度是 O ( 1 ) O(1) O(1)。
空间复杂度: O ( n ) O(n) O(n),其中 n n n 是请求次数。空间复杂度主要取决于队列空间,队列内存储最近 3000 3000 3000 毫秒的请求,空间复杂度是 O ( n ) O(n) O(n)。
边栏推荐
- QStyle platform style
- 解决端口占用问题 Port xxxx was already in use
- 倒计时 2 天|云原生 Meetup 广州站,等你来!
- 用CH341A烧录外挂Flash (W25Q16JV)
- 十五. 实战——mysql建库建表 字符集 和 排序规则
- On governance and innovation, the 2022 OpenAtom Global Open Source Summit OpenAnolis sub-forum came to a successful conclusion
- STM32 uses stm32cubemx LL library series tutorial
- How to sort multiple fields and multiple values in sql statement
- Object.defineProperty monitors data changes in real time and updates the page
- 如何在WordPress中添加特定类别的小工具
猜你喜欢

How Jin Cang database correctness verification platform installation file

2022高处安装、维护、拆除考试题模拟考试题库及在线模拟考试

Intersection of Boolean Operations in SuperMap iDesktop.Net - Repairing Complex Models with Topological Errors

Countdown to 2 days|Cloud native Meetup Guangzhou Station, waiting for you!

How to sort multiple fields and multiple values in sql statement

.NET应用程序--Helloworld(C#)
![[Qixi Festival] Romantic Tanabata, code teaser.Turn love into a gorgeous three-dimensional scene and surprise her (him)!(send code)](/img/10/dafea90158adf9d43c4f025414fef7.png)
[Qixi Festival] Romantic Tanabata, code teaser.Turn love into a gorgeous three-dimensional scene and surprise her (him)!(send code)

dmp(dump)转储文件

【 genius_platform software platform development 】 : seventy-six vs the preprocessor definitions written cow force!!!!!!!!!!(in the other groups conding personnel told so cow force configuration to can

Data to enhance Mixup principle and code reading
随机推荐
用CH341A烧录外挂Flash (W25Q16JV)
private package
QT language file production
AI+PROTAC | dx/tx completes $5 million seed round
Everyone in China said data, you need to focus on core characteristic is what?
通过模拟Vite一起深入其工作原理
The linear table lookup
语法基础(变量、输入输出、表达式与顺序语句完成情况)
Apache DolphinScheduler, a new generation of distributed workflow task scheduling platform in practice - Medium
(十一)元类
Solve the problem of port occupancy Port xxxx was already in use
sql server 安装提示用户名不存在
AI + Small Nucleic Acid Drugs | Eleven Completes $22 Million Seed Round Financing
Open Source License Description LGPL
【七夕节】浪漫七夕,代码传情。将爱意变成绚烂的立体场景,给她(他)一个惊喜!(送代码)
1667. 修复表中的名字
1484. Sell Products by Date
mysql没法Execute 大拿们求解
Likou - preorder traversal, inorder traversal, postorder traversal of binary tree
leetcode - symmetric binary tree