当前位置:网站首页>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).
边栏推荐
- Simple description of linked list and simple implementation of code
- Web3.0 Dapps——通往未来金融世界的道路
- In 2022, you still can't "low code"?Data science can also play with Low-Code!
- Linux下常见的开源数据库,你知道几个?
- 【 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
- 软链接引发的物理备份问题
- [Solved] Unity Coroutine coroutine is not executed effectively
- YYGH-13-客服中心
- 21天学习挑战赛(2)图解设备树的使用
- Syntax basics (variables, input and output, expressions and sequential statements)
猜你喜欢
ASP.NET应用程序--Hello World
告白数字化转型时代,时速云镌刻价值新起点
After the large pixel panorama is completed, what are the promotion methods?
Ant Sword Advanced Module Development
In 2022, you still can't "low code"?Data science can also play with Low-Code!
论治理与创新,2022 开放原子全球开源峰会 OpenAnolis 分论坛圆满落幕
【软件测试】自动化测试之unittest框架
使用二维码传输文件的小工具 - QFileTrans 1.2.0.1
Matlab drawing 3
word分栏小记
随机推荐
(十一)元类
Common open source databases under Linux, how many do you know?
Native js realizes the effect of selecting and canceling all the multi-select boxes
UOS系统下ksql应用缺少动态库”libtinfo.so.5“问题
【 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
Never put off till tomorrow what you can put - house lease management system based on the SSM
Chinese characters to Pinyin
Package zip is not available, but is referred to by another package.
Apache DolphinScheduler, a new generation of distributed workflow task scheduling platform in practice - Medium
【已解决】Unity Coroutinue 协程未有效执行的问题
dmp (dump) dump file
【七夕节】浪漫七夕,代码传情。将爱意变成绚烂的立体场景,给她(他)一个惊喜!(送代码)
QT language file production
Principle and Technology of Virtual Memory
STM32 uses stm32cubemx LL library series tutorial
public static <T> List<T> asList(T... a) 原型是怎么回事?
torch.roll()
How to solve the error cannot update secondary snapshot during a parallel operation when the PostgreSQL database uses navicat to open the table structure?
Distributed systems revisited: there will never be a perfect consistency scheme...
龙蜥社区第二届理事大会圆满召开!理事换届选举、4 位特约顾问加入