当前位置:网站首页>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)
边栏推荐
- TCP拥塞控制详解 | 2. 背景
- Fuyuan medicine is listed on the Shanghai Stock Exchange: the market value is 10.5 billion, and Hu Baifan is worth more than 4billion
- Blog theme "text" summer fresh Special Edition
- 社交元宇宙平台Soul冲刺港股:年营收12.8亿 腾讯是股东
- Notice on holding a salon for young editors of scientific and Technological Journals -- the abilities and promotion strategies that young editors should have in the new era
- [shutter] dart data type (dynamic data type)
- Error when uploading code to remote warehouse: remote origin already exists
- VMware install win10 image
- PhD battle-11 preview | review and prospect backdoor attack and defense of neural network
- [essay solicitation activity] Dear developer, RT thread community calls you to contribute
猜你喜欢

Qwebengineview crash and alternatives

QWebEngineView崩溃及替代方案

Sword finger offer 25 Merge two sorted linked lists

如何与博格华纳BorgWarner通过EDI传输业务数据?
![[shutter] dart data type (dynamic data type)](/img/6d/60277377852294c133b94205066e9e.jpg)
[shutter] dart data type (dynamic data type)

剑指 Offer 22. 链表中倒数第k个节点

Does digicert SSL certificate support Chinese domain name application?

Ap和F107数据来源及处理

福元医药上交所上市:市值105亿 胡柏藩身价超40亿

Sword finger offer 24 Reverse linked list
随机推荐
How to quickly distinguish controlled components from uncontrolled components?
P6774 [NOI2020] 时代的眼泪(分块)
寒门再出贵子:江西穷县考出了省状元,做对了什么?
深度之眼(二)——矩阵及其基本运算
The macrogenome microbiome knowledge you want is all here (2022.7)
ETH数据集下载及相关问题
ThreadLocal
MOSFET器件手册关键参数解读
2022 interview questions
ThreadLocal
OpenHarmony如何启动远程设备的FA
System Verilog implements priority arbiter
亚马逊云科技 Community Builder 申请窗口开启
LSF basic command
七张图,学会做有价值的经营分析
体验居家办公完成项目有感 | 社区征文
R and rstudio download and installation tutorial (super detailed)
Sword finger offer 25 Merge two sorted linked lists
Detailed explanation of @accessories annotation of Lombok plug-in
Use the API port of the bridge of knowledge and action to provide resources for partners to access