当前位置:网站首页>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).
边栏推荐
- How to sort multiple fields and multiple values in sql statement
- 剑指Offer--找出数组中重复的数字(三种解法)
- Syntax basics (variables, input and output, expressions and sequential statement completion)
- public static <T> List<T> asList(T... a) 原型是怎么回事?
- Beidou no. 3 short message terminal high slope in open-pit mine monitoring programme
- 引领数字医学高地,中山医院探索打造未来医院“新范式”
- 为什么pca分量没有关联
- 调用阿里云oss和sms服务
- presto启动成功后出现2022-08-04T17:50:58.296+0800 ERROR Announcer-3 io.airlift.discovery.client.Announcer
- ffmpeg -sources分析
猜你喜欢

Never put off till tomorrow what you can put - house lease management system based on the SSM

Apache DolphinScheduler, a new generation of distributed workflow task scheduling platform in practice - Medium

IJCAI2022 | DictBert: Pre-trained Language Models with Contrastive Learning for Dictionary Description Knowledge Augmentation

冰蝎V4.0攻击来袭,安全狗产品可全面检测

The Tanabata copywriting you want has been sorted out for you!

Leading the highland of digital medicine, Zhongshan Hospital explores to create a "new paradigm" for future hospitals

The second council meeting of the Dragon Lizard Community was successfully held!Director general election, 4 special consultants joined

ASP.NET应用程序--Hello World

今年七夕,「情蔬」比礼物更有爱

毕设-基于SSM房屋租赁管理系统
随机推荐
Chinese characters to Pinyin
HDU 1114: Piggy-Bank ← The Complete Knapsack Problem
链表的简单描述及代码的简单实现
Study Notes-----Left-biased Tree
语法基础(变量、输入输出、表达式与顺序语句)
How to sort multiple fields and multiple values in sql statement
2022 High-level installation, maintenance, and removal of exam questions mock exam question bank and online mock exam
论治理与创新,2022 开放原子全球开源峰会 OpenAnolis 分论坛圆满落幕
Thinking (88): Use protobuf custom options for multi-version management of data
Turn: Charles Handy: Who you are is more important than what you do
调用阿里云oss和sms服务
Question about #sql shell#, how to solve it?
静态方法获取配置文件数据
A small tool to transfer files using QR code - QFileTrans 1.2.0.1
QStyle platform style
mysql can't Execute, please solve it
Ant Sword Advanced Module Development
One hundred - day plan -- -- DAY2 brush
Snapback - same tree
mysql没法Execute 大拿们求解