当前位置:网站首页>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).
边栏推荐
- 今年七夕,「情蔬」比礼物更有爱
- Is your data safe in this hyperconnected world?
- Use @Mapper to query the partition status of oracle and report an error
- Data storage practice based on left-order traversal
- private封装
- 用Unity发布APP到Hololens2无坑教程
- 金仓数据库如何验证安装文件平台正确性
- The second council meeting of the Dragon Lizard Community was successfully held!Director general election, 4 special consultants joined
- tree table lookup
- High Item 02 Information System Project Management Fundamentals
猜你喜欢

QT language file production

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

How Jin Cang database correctness verification platform installation file

运维监控系统之Open-Falcon

How to Add Category-Specific Widgets in WordPress

引领数字医学高地,中山医院探索打造未来医院“新范式”

龙蜥社区第二届理事大会圆满召开!理事换届选举、4 位特约顾问加入

Is your data safe in this hyperconnected world?

Beidou no. 3 short message terminal high slope in open-pit mine monitoring programme

Principle and Technology of Virtual Memory
随机推荐
金仓数据库如何验证安装文件平台正确性
The problem of lack of dynamic library "libtinfo.so.5" in ksql application under UOS system
为什么pca分量没有关联
AI+PROTAC | dx/tx completes $5 million seed round
Principle and Technology of Virtual Memory
Flink 1.15.1 Cluster Construction (StandaloneSession)
CPDA|How Operators Learn Data Analysis (SQL) from Negative Foundations
队列题目:最近的请求次数
(11) Metaclass
通过模拟Vite一起深入其工作原理
运维监控系统之Open-Falcon
Everyone in China said data, you need to focus on core characteristic is what?
private package
北斗三号短报文终端露天矿山高边坡监测方案
Intersection of Boolean Operations in SuperMap iDesktop.Net - Repairing Complex Models with Topological Errors
AI + Small Nucleic Acid Drugs | Eleven Completes $22 Million Seed Round Financing
QT: The Magical QVarient
Physical backup issues caused by soft links
How to sort multiple fields and multiple values in sql statement
引领数字医学高地,中山医院探索打造未来医院“新范式”