当前位置:网站首页>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).
边栏推荐
- 数学-求和符号的性质
- 2022了你还不会『低代码』?数据科学也能玩转Low-Code啦!
- Beyond YOLO5-Face | YOLO-FaceV2 officially open source Trick+ academic point full
- private封装
- From "useable" to "easy to use", domestic software is self-controllable and continues to advance
- One hundred - day plan -- -- DAY2 brush
- Summary of domestic environments supported by SuperMap
- 2022高处安装、维护、拆除考试题模拟考试题库及在线模拟考试
- Talking about data security governance and privacy computing
- .NET Application -- Helloworld (C#)
猜你喜欢

word column notes

如何在WordPress中添加特定类别的小工具

The usage of try...catch and finally in js

Flink 1.15.1 Cluster Construction (StandaloneSession)

ASP.NET application--Hello World

人人都在说的数据中台,你需要关注的核心特点是什么?

Simple description of linked list and simple implementation of code

Intersection of Boolean Operations in SuperMap iDesktop.Net - Repairing Complex Models with Topological Errors
![[Solved] Unity Coroutine coroutine is not executed effectively](/img/ab/035ef004a561fb98d3dd1d7d8b5618.png)
[Solved] Unity Coroutine coroutine is not executed effectively

2022高处安装、维护、拆除考试题模拟考试题库及在线模拟考试
随机推荐
[论文笔记] MapReduce: Simplified Data Processing on Large Clusters
Principle and Technology of Virtual Memory
冒泡排序与快速排序
基于生长的棋盘格角点检测方法
从“能用”到“好用” 国产软件自主可控持续推进
Why did they choose to fall in love with AI?
[Solved] Unity Coroutine coroutine is not executed effectively
MRTK3开发Hololens应用-手势拖拽、旋转 、缩放物体实现
undo问题
HDU 1114: Piggy-Bank ← The Complete Knapsack Problem
Linux下常见的开源数据库,你知道几个?
QT MV\MVC structure
tree table lookup
sql怎么找字段里所有数据为空的字段
ffmpeg 枚举decoders, encoders 分析
Review 51 MCU
语法基础(变量、输入输出、表达式与顺序语句完成情况)
北斗三号短报文终端露天矿山高边坡监测方案
龙蜥社区第二届理事大会圆满召开!理事换届选举、4 位特约顾问加入
思考(八十八):使用 protobuf 自定义选项,做数据多版本管理