当前位置:网站首页>Leetcode question brushing record | 933_ Recent requests
Leetcode question brushing record | 933_ Recent requests
2022-07-02 17:17:00 【coder_ sure】
leetcode Record of writing questions |933 _ Recent requests
author github link : github link
Force to buckle 933 topic
type : queue
subject :
Write a RecentCounter Class to calculate the most recent request in a specific time range .
Please realize RecentCounter class :
RecentCounter()Initialize counter , The number of requests is0.int ping(int t)In timetAdd a new request , amongtRepresents a time in milliseconds , And go back to the past3000The number of all requests in milliseconds( Including new requests ). To be precise , Back in the[t-3000, t]The number of requests that occurred in .
Guarantee Every time thepingAll calls to thetvalue .
Example 1
Input :
["RecentCounter", "ping", "ping", "ping", "ping"]
[[], [1], [100], [3001], [3002]]
Output :
[null, 1, 2, 3, 3]
explain :
RecentCounter recentCounter = new RecentCounter();
recentCounter.ping(1); // requests = [1], The scope is [-2999,1], return 1
recentCounter.ping(100); // requests = [1, 100], The scope is [-2900,100], return 2
recentCounter.ping(3001); // requests = [1, 100, 3001], The scope is [1,3001], return 3
recentCounter.ping(3002); // requests = [1, 100, 3001, 3002], The scope is [2,3002], return 3
Their thinking
Train of thought details :
- Define a queue , Used to load request time
- Load the request time into the queue one by one
- Each time, judge whether the difference between the time when the queue now enters the queue and the time at the beginning of the queue is greater than 3000 La !
- Greater than 3000, Just make it out of the queue at the beginning of the queue , Go back and judge whether it is greater than 3000.
- If not greater than 3000, The length of the return queue is the desired .
c++
class RecentCounter {
public:
queue<int> Q;
RecentCounter() {
}
int ping(int t) {
Q.push(t);
while(Q.size()>0 and t-Q.front()>3000){
Q.pop();
}
return Q.size();
}
};
/** * Your RecentCounter object will be instantiated and called as such: * RecentCounter* obj = new RecentCounter(); * int param_1 = obj->ping(t); */
python
class RecentCounter:
def __init__(self):
self.Q = collections.deque()
def ping(self, t: int) -> int:
self.Q.append(t)
while len(self.Q)>0 and t-self.Q[0]>3000:
self.Q.popleft()
return len(self.Q)
边栏推荐
- Believe in yourself and finish the JVM interview this time
- DigiCert SSL证书支持中文域名申请吗?
- Use of openpose
- 剑指 Offer 21. 调整数组顺序使奇数位于偶数前面
- What is generics- Introduction to generics
- Connect Porsche and 3PL EDI cases
- 剑指 Offer 27. 二叉树的镜像
- 2322. Remove the minimum fraction of edges from the tree (XOR and & Simulation)
- L'explosion de John utilise l'encodage d'entrée par défaut: UTF - 8 Loaded 1 password Hash (bcrypt [blowfish 32 / 64 X3])
- ThreadLocal
猜你喜欢

Believe in yourself and finish the JVM interview this time

Green bamboo biological sprint Hong Kong stocks: loss of more than 500million during the year, tiger medicine and Beijing Yizhuang are shareholders

Use of openpose

Jiuxian's IPO was terminated: Sequoia and Dongfang Fuhai were shareholders who had planned to raise 1billion yuan

Just a coincidence? The mysterious technology of apple ios16 is even consistent with the products of Chinese enterprises five years ago!

ETH数据集下载及相关问题

Fuyuan medicine is listed on the Shanghai Stock Exchange: the market value is 10.5 billion, and Hu Baifan is worth more than 4billion

智能垃圾桶(五)——点亮OLED
![John blasting appears using default input encoding: UTF-8 loaded 1 password hash (bcrypt [blowfish 32/64 x3])](/img/4c/ddf7f8085257d0eb8766dbec251345.png)
John blasting appears using default input encoding: UTF-8 loaded 1 password hash (bcrypt [blowfish 32/64 x3])

Understand one article: four types of data index system
随机推荐
QStyle实现自绘界面项目实战(二)
ssb门限_SSB调制「建议收藏」
一文看懂:数据指标体系的4大类型
2022 interview questions
VMware安装win10镜像
Deep learning image data automatic annotation [easy to understand]
What if the default browser cannot be set?
Domestic relatively good OJ platform [easy to understand]
体验居家办公完成项目有感 | 社区征文
博客主题 “Text“ 夏日清新特别版
IP地址转换地址段
学习周刊-总第60期-2022年第25周
易语言abcd排序
OpenPose的使用
人生的开始
P6774 [NOI2020] 时代的眼泪(分块)
The poor family once again gave birth to a noble son: Jiangxi poor county got the provincial number one, what did you do right?
关于举办科技期刊青年编辑沙龙——新时代青年编辑应具备的能力及提升策略的通知...
C语言中sprintf()函数的用法
绿竹生物冲刺港股:年期内亏损超5亿 泰格医药与北京亦庄是股东