当前位置:网站首页>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 timet
Add a new request , amongt
Represents a time in milliseconds , And go back to the past3000
The 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 theping
All calls to thet
value .
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)
边栏推荐
- What will you do after digital IC Verification?
- 二、mock平台的扩展
- 入行数字IC验证后会做些什么?
- Use the API port of the bridge of knowledge and action to provide resources for partners to access
- Connect Porsche and 3PL EDI cases
- js删除字符串中的子串
- VMware安装win10镜像
- Geoserver: publishing PostGIS data sources
- Exploration of mobile application performance tools
- One year is worth ten years
猜你喜欢
Qwebengineview crash and alternatives
畅玩集团冲刺港股:年营收2.89亿 刘辉有53.46%投票权
【Leetcode】14. 最长公共前缀
Sword finger offer 26 Substructure of tree
剑指 Offer 22. 链表中倒数第k个节点
剑指 Offer 27. 二叉树的镜像
Cell:清华程功组揭示皮肤菌群的一种气味挥发物促进黄病毒感染宿主吸引蚊虫...
[error record] error -32000 received from application: there are no running service protocol
A case study of college entrance examination prediction based on multivariate time series
对接保时捷及3PL EDI案例
随机推荐
Sword finger offer 24 Reverse linked list
Atcoder beginer contest 169 (B, C, D unique decomposition, e mathematical analysis f (DP))
Method of C language self defining function
Timing / counter of 32 and 51 single chip microcomputer
L'explosion de John utilise l'encodage d'entrée par défaut: UTF - 8 Loaded 1 password Hash (bcrypt [blowfish 32 / 64 X3])
PhD Debate-11 预告 | 回顾与展望神经网络的后门攻击与防御
What is the difference between JSP and servlet?
超卓航科上市:募资9亿市值超60亿 成襄阳首家科创板企业
&lt; IV & gt; H264 decode output YUV file
Dgraph: large scale dynamic graph dataset
IP address translation address segment
Un an à dix ans
移动应用性能工具探索之路
Tech talk activity preview | building intelligent visual products based on Amazon kVs
871. 最低加油次数
Use of openpose
Does digicert SSL certificate support Chinese domain name application?
Nexus简介及小白使用IDEA打包上传到Nexus3私服详细教程
System Verilog implements priority arbiter
Amazon cloud technology community builder application window opens