当前位置:网站首页>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)
边栏推荐
猜你喜欢
Chapter 3 of hands on deep learning - (1) linear regression is realized from scratch_ Learning thinking and exercise answers
剑指 Offer 21. 调整数组顺序使奇数位于偶数前面
The macrogenome microbiome knowledge you want is all here (2022.7)
john爆破出現Using default input encoding: UTF-8 Loaded 1 password hash (bcrypt [Blowfish 32/64 X3])
[cloud native] briefly talk about the understanding of flume, a massive data collection component
Tech talk activity preview | building intelligent visual products based on Amazon kVs
Cell:清华程功组揭示皮肤菌群的一种气味挥发物促进黄病毒感染宿主吸引蚊虫...
Win10系统使用pip安装juypter notebook过程记录(安装在系统盘以外的盘)
关于举办科技期刊青年编辑沙龙——新时代青年编辑应具备的能力及提升策略的通知...
相信自己,这次一把搞定JVM面试
随机推荐
Green bamboo biological sprint Hong Kong stocks: loss of more than 500million during the year, tiger medicine and Beijing Yizhuang are shareholders
Configure ARP table entry restrictions and port security based on the interface (restrict users' private access to fool switches or illegal host access)
Win10系统使用pip安装juypter notebook过程记录(安装在系统盘以外的盘)
剑指 Offer 27. 二叉树的镜像
Amazon cloud technology community builder application window opens
R and rstudio download and installation tutorial (super detailed)
綠竹生物沖刺港股:年期內虧損超5億 泰格醫藥與北京亦莊是股東
uboot的作用和功能
伟立控股港交所上市:市值5亿港元 为湖北贡献一个IPO
linux下配置Mysql授权某个用户远程访问,不受ip限制
Youzan won the "top 50 Chinese enterprise cloud technology service providers" together with Tencent cloud and Alibaba cloud [easy to understand]
畅玩集团冲刺港股:年营收2.89亿 刘辉有53.46%投票权
Baobab's gem IPO was terminated: Tang Guangyu once planned to raise 1.8 billion to control 47% of the equity
DigiCert SSL证书支持中文域名申请吗?
Fuyuan medicine is listed on the Shanghai Stock Exchange: the market value is 10.5 billion, and Hu Baifan is worth more than 4billion
Understand one article: four types of data index system
Geoserver: publishing PostGIS data sources
绿竹生物冲刺港股:年期内亏损超5亿 泰格医药与北京亦庄是股东
871. Minimum refueling times
体验居家办公完成项目有感 | 社区征文