当前位置:网站首页>Queue Topic: Recent Requests
Queue Topic: Recent Requests
2022-08-05 03:19:00 【the great Czerny】
题目
标题和出处
标题:最近的请求次数
出处: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 是严格单调递增的,Therefore, storing request times in the order of invocation results in a strictly increasing sequence of request times.每次调用 ping \textit{ping} ping The method asks to go back to the past 3000 3000 3000 毫秒内发生的所有请求数,So the distance request time in the request time series can be exceeded 3000 3000 3000 milliseconds for request deletion,Then count the number of requests in the request time series,the past 3000 3000 3000 毫秒内发生的所有请求数.
Because the oldest request will be deleted first,Therefore, the request time series meets the characteristics of first-in, first-out,Request time series can be implemented using queues,在构造方法中初始化队列.
对于 ping \textit{ping} ping 方法,Dequeue stale requests first,If the queue is not empty and the head of the queue is more than the current request time 3000 3000 3000 毫秒,则将队首元素出队列,This operation is repeated until the queue becomes empty or the head of the queue is within the current request time 3000 3000 3000 毫秒.然后将当前请求时间入队列,At this time, the number of elements in the queue is the past 3000 3000 3000 毫秒内发生的所有请求数,Return the number of elements in the queue.
代码
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).Each element is enqueued and dequeued at most once,因此方法 ping \textit{ping} ping 的均摊时间复杂度是 O ( 1 ) O(1) O(1).
空间复杂度: O ( n ) O(n) O(n),其中 n n n is the number of requests.The space complexity mainly depends on the queue space,The most recent is stored in the queue 3000 3000 3000 毫秒的请求,空间复杂度是 O ( n ) O(n) O(n).
边栏推荐
- Tencent Cloud [Hiflow] New Era Automation Tool
- 龙蜥社区第二届理事大会圆满召开!理事换届选举、4 位特约顾问加入
- How to Add Category-Specific Widgets in WordPress
- (十一)元类
- PostgreSQL数据库 用navicat 打开表结构的时候报错 cannot update secondarysnapshot during a parallel operation 怎么解决?
- 高项 02 信息系统项目管理基础
- Native js realizes the effect of selecting and canceling all the multi-select boxes
- Solve the problem of port occupancy Port xxxx was already in use
- Chinese characters to Pinyin
- The pit of std::string::find return value
猜你喜欢
YYGH-13-客服中心
Leading the highland of digital medicine, Zhongshan Hospital explores to create a "new paradigm" for future hospitals
presto启动成功后出现2022-08-04T17:50:58.296+0800 ERROR Announcer-3 io.airlift.discovery.client.Announcer
静态方法获取配置文件数据
Dynamic management of massive service instances
Ant Sword Advanced Module Development
Beidou no. 3 short message terminal high slope in open-pit mine monitoring programme
北斗三号短报文终端露天矿山高边坡监测方案
引领数字医学高地,中山医院探索打造未来医院“新范式”
金仓数据库如何验证安装文件平台正确性
随机推荐
达梦8数据库导出导入
思考(八十八):使用 protobuf 自定义选项,做数据多版本管理
用CH341A烧录外挂Flash (W25Q16JV)
leetcode - a subtree of another tree
rpc-remote procedure call demo
[论文笔记] MapReduce: Simplified Data Processing on Large Clusters
Summary of domestic environments supported by SuperMap
QT language file production
1667. Fix names in tables
MRTK3开发Hololens应用-手势拖拽、旋转 、缩放物体实现
How to Add Category-Specific Widgets in WordPress
The pit of std::string::find return value
21 Days Learning Challenge (2) Use of Graphical Device Trees
On governance and innovation, the 2022 OpenAtom Global Open Source Summit OpenAnolis sub-forum came to a successful conclusion
Intersection of Boolean Operations in SuperMap iDesktop.Net - Repairing Complex Models with Topological Errors
Web3.0 Dapps——通往未来金融世界的道路
冒泡排序与快速排序
【 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
CPDA|How Operators Learn Data Analysis (SQL) from Negative Foundations
Turn: Charles Handy: Who you are is more important than what you do