当前位置:网站首页>Force deduction solution summary 933- number of recent requests
Force deduction solution summary 933- number of recent requests
2022-06-12 02:08:00 【Lost summer】
Directory links :
Force buckle programming problem - The solution sums up _ Share + Record -CSDN Blog
GitHub Synchronous question brushing items :
https://github.com/September26/java-algorithms
Original link : Power button
describe :
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 is 0 .
int ping(int t) In time t Add a new request , among t Represents a time in milliseconds , And go back to the past 3000 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 the ping All calls to the t 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
Tips :
1 <= t <= 109
Make sure you do it every time ping The... Used by the call t It's worth it Strictly increasing
Call at most ping Method 104 Time
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/number-of-recent-calls
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Their thinking :
* Their thinking : * It is most appropriate to store the results of the queue , Because the title provides t Is increasing , So every time you add numbers, you start traversing from the front of the queue , If it does not meet the scope, remove , Otherwise, exit the loop . * Finally, just return the number of queues .
Code :
public class Solution933 {
class RecentCounter {
Queue<Integer> queue = new ArrayDeque<>();
public RecentCounter() {
}
public int ping(int t) {
int i = t - 3000;
Iterator<Integer> iterator = queue.iterator();
while (iterator.hasNext()) {
Integer next = iterator.next();
if (next < i) {
iterator.remove();
continue;
}
break;
}
queue.offer(t);
return queue.size();
}
}
}边栏推荐
- 力扣解法汇总433-最小基因变化
- 高考完不要急着去打工了,打工以后有的是机会,不差这三个月
- 力扣解法汇总1037-有效的回旋镖
- ACL 2022 | 预训练语言模型和图文模型的强强联合
- Wide match modifier symbol has been deprecated, do not use
- matplotlib. pyplot. Bar chart (II)
- 力扣解法汇总732-我的日程安排表 III
- 消防栓监测系统毕业设计---论文(附加最全面的从硬件电路设计->驱动程序设计->阿里云物联网搭建->安卓APP设计)
- Installing MySQL version 5.5 database for Linux (centos6)
- Database
猜你喜欢

php开发 博客系统的公告模块的建立和引入

Don't miss it! Five large data visualization screens that HR must collect

Basedexclassloader

Smartbi helps you solve the problem of losing high-value customers

Alicloud OSS file upload system

Explore performance optimization! Performance improvement from 2 months to 4 hours!

Linux(CentOS6)安装MySQL5.5版本数据库

Graphical data analysis | data analysis tool map

Is there a female Bluetooth headset suitable for girls? 38 Bluetooth headsets worth getting started

Ozzanation - système d'action basé sur sse
随机推荐
Installing MySQL version 5.5 database for Linux (centos6)
力扣解法汇总497-非重叠矩形中的随机点
php开发 博客系统的公告模块的建立和引入
力扣解法汇总386-字典序排数
Force deduction solution summary 905- array sorted by parity
力扣解法汇总467-环绕字符串中唯一的子字符串
力扣解法汇总668-乘法表中第k小的数
力扣解法汇总433-最小基因变化
Force deduction solution summary 1728- cat and mouse II
Force deduction solution summary 396 rotation function
MySQL高级部分知识点
如何提高广告的广告评级,也就是质量得分?
Advantages of Google ads
力扣解法汇总-剑指 Offer II 114. 外星文字典
PHP development 09 article module deletion and article classification writing
Summary of force deduction method 417- Pacific Atlantic current problems
力扣解法汇总675-为高尔夫比赛砍树
力扣解法汇总436-寻找右区间
PHP builds a high-performance API architecture based on sw-x framework (III)
Summary of concrete (ground + wall) + Mountain crack data set (classification and target detection)