当前位置:网站首页>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 is generics- Introduction to generics
- 人生的开始
- Ap和F107数据来源及处理
- 2022 interview questions
- Sword finger offer 21 Adjust the array order so that odd numbers precede even numbers
- PhD battle-11 preview | review and prospect backdoor attack and defense of neural network
- 寒门再出贵子:江西穷县考出了省状元,做对了什么?
- How to quickly distinguish controlled components from uncontrolled components?
- Sword finger offer 27 Image of binary tree
- QStyle实现自绘界面项目实战(二)
猜你喜欢
Cell: Tsinghua Chenggong group revealed an odor of skin flora. Volatiles promote flavivirus to infect the host and attract mosquitoes
Sword finger offer 25 Merge two sorted linked lists
深度之眼(三)——矩阵的行列式
Believe in yourself and finish the JVM interview this time
【Leetcode】13. Roman numeral to integer
Does digicert SSL certificate support Chinese domain name application?
linux安装postgresql + patroni 集群问题
ETH数据集下载及相关问题
ThreadLocal
Listing of chaozhuo Aviation Technology Co., Ltd.: raising 900million yuan, with a market value of more than 6billion yuan, becoming the first science and technology innovation board enterprise in Xia
随机推荐
有赞和腾讯云、阿里云一同摘得“中国企业云科技服务商50强”[通俗易懂]
MOSFET器件手册关键参数解读
vscode设置删除行快捷键[通俗易懂]
A few lines of code to complete RPC service registration and discovery
Green bamboo biological sprint Hong Kong stocks: loss of more than 500million during the year, tiger medicine and Beijing Yizhuang are shareholders
剑指 Offer 24. 反转链表
远程办公对我们的各方面影响心得 | 社区征文
如何与博格华纳BorgWarner通过EDI传输业务数据?
L'explosion de John utilise l'encodage d'entrée par défaut: UTF - 8 Loaded 1 password Hash (bcrypt [blowfish 32 / 64 X3])
深度之眼(三)——矩阵的行列式
class和getClass()的区别
AP and F107 data sources and processing
Use the API port of the bridge of knowledge and action to provide resources for partners to access
剑指 Offer 27. 二叉树的镜像
What if the default browser cannot be set?
How to quickly distinguish controlled components from uncontrolled components?
Usage of sprintf() function in C language
Atcoder beginer contest 169 (B, C, D unique decomposition, e mathematical analysis f (DP))
Configure MySQL under Linux to authorize a user to access remotely, which is not restricted by IP
綠竹生物沖刺港股:年期內虧損超5億 泰格醫藥與北京亦莊是股東