当前位置:网站首页>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)
边栏推荐
猜你喜欢

智能垃圾桶(五)——点亮OLED

【Leetcode】14. 最長公共前綴

【Leetcode】14. 最长公共前缀

Executive engine module of high performance data warehouse practice based on Impala

Blog theme "text" summer fresh Special Edition
![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])

QWebEngineView崩溃及替代方案

Weili holdings listed on the Hong Kong Stock Exchange: with a market value of HK $500million, it contributed an IPO to Hubei

linux下配置Mysql授权某个用户远程访问,不受ip限制

剑指 Offer 27. 二叉树的镜像
随机推荐
对接保时捷及3PL EDI案例
The poor family once again gave birth to a noble son: Jiangxi poor county got the provincial number one, what did you do right?
深度之眼(二)——矩阵及其基本运算
关于举办科技期刊青年编辑沙龙——新时代青年编辑应具备的能力及提升策略的通知...
Configure ARP table entry restrictions and port security based on the interface (restrict users' private access to fool switches or illegal host access)
Usage of sprintf() function in C language
A case study of college entrance examination prediction based on multivariate time series
Sword finger offer 27 Image of binary tree
QWebEngineView崩溃及替代方案
Amazon cloud technology community builder application window opens
2022 interview questions
易语言abcd排序
Error when uploading code to remote warehouse: remote origin already exists
Linux Installation PostgreSQL + Patroni cluster problem
linux安装postgresql + patroni 集群问题
How to quickly distinguish controlled components from uncontrolled components?
一年顶十年
What is generics- Introduction to generics
Jiuxian's IPO was terminated: Sequoia and Dongfang Fuhai were shareholders who had planned to raise 1billion yuan
IP地址转换地址段