当前位置:网站首页>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)
边栏推荐
- 博客主题 “Text“ 夏日清新特别版
- 配置基于接口的ARP表项限制和端口安全(限制用户私自接入傻瓜交换机或非法主机接入)
- Detailed explanation of @accessories annotation of Lombok plug-in
- Digital IC hand tearing code -- voting device
- 2020 "Lenovo Cup" National College programming online Invitational Competition and the third Shanghai University of technology programming competition (a sign in, B sign in, C sign in, D thinking +mst
- jsp 和 servlet 有什么区别?
- 一年顶十年
- 剑指 Offer 25. 合并两个排序的链表
- 剑指 Offer 26. 树的子结构
- Atcoder beginer contest 169 (B, C, D unique decomposition, e mathematical analysis f (DP))
猜你喜欢

Cell: Tsinghua Chenggong group revealed an odor of skin flora. Volatiles promote flavivirus to infect the host and attract mosquitoes

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

默认浏览器设置不了怎么办?

Chapter 3 of hands on deep learning - (1) linear regression is realized from scratch_ Learning thinking and exercise answers
![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])

PhD battle-11 preview | review and prospect backdoor attack and defense of neural network

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

如何与博格华纳BorgWarner通过EDI传输业务数据?

Connect Porsche and 3PL EDI cases

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
随机推荐
Detailed explanation of @accessories annotation of Lombok plug-in
QWebEngineView崩溃及替代方案
ssb门限_SSB调制「建议收藏」
PhD battle-11 preview | review and prospect backdoor attack and defense of neural network
What is generics- Introduction to generics
綠竹生物沖刺港股:年期內虧損超5億 泰格醫藥與北京亦莊是股東
The poor family once again gave birth to a noble son: Jiangxi poor county got the provincial number one, what did you do right?
Digital IC hand tearing code -- voting device
IP地址转换地址段
你想要的宏基因组-微生物组知识全在这(2022.7)
一文看懂:数据指标体系的4大类型
Un an à dix ans
2022 interview questions
Changwan group rushed to Hong Kong stocks: the annual revenue was 289million, and Liu Hui had 53.46% voting rights
Configure ARP table entry restrictions and port security based on the interface (restrict users' private access to fool switches or illegal host access)
P6774 [NOI2020] 时代的眼泪(分块)
The impact of telecommuting on all aspects of our experience | community essay solicitation
In MySQL and Oracle, the boundary and range of between and precautions when querying the date
js删除字符串中的子串
R and rstudio download and installation tutorial (super detailed)