[LeetCode] 933. Number of Recent Calls 最近的调用次数
Write a class `RecentCounter` to count recent requests.
It has only one method: ping(int t), where t represents some time in milliseconds.
Return the number of pings that have been made from 3000 milliseconds ago until now.
Any ping with time in [t - 3000, t] will count, including the current ping.
It is guaranteed that every call to ping uses a strictly larger value of t than before.
Example 1:
Input: inputs = ["RecentCounter","ping","ping","ping","ping"], inputs = [[],[1],[100],[3001],[3002]]
Output: [null,1,2,3,3]
Note:
- Each test case will have at most
10000calls toping. - Each test case will call
pingwith strictly increasing values oft. - Each call to ping will have
1 <= t <= 10^9.
这道题让实现一个 RecentCounter 类,里面有一个 ping 函数,输入给定了一个时间t,让我们求在 [t-3000, t] 时间范围内有多少次 ping。题目中限定了每次的给的时间一定会比上一次的时间大,而且只关心这个大小为 3001 的时间窗口范围内的次数,则利用滑动窗口 Sliding Window 来做就是个很不错的选择。由于数字是不断加入的,可以使用一个 queue,每当要加入一个新的时间点t时,先从队列开头遍历,若前面的时间不在当前的时间窗口内,则移除队列。之后再将当前时间点t加入,并返回队列的长度即可,参见代码如下:
class RecentCounter {
public:
RecentCounter() {}
int ping(int t) {
while (!q.empty()) {
if (q.front() + 3000 >= t) break;
q.pop();
}
q.push(t);
return q.size();
}
private:
queue<int> q;
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/933
参考资料:
https://leetcode.com/problems/number-of-recent-calls/
[LeetCode All in One 题目讲解汇总(持续更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)
[LeetCode] 933. Number of Recent Calls 最近的调用次数的更多相关文章
- 【Leetcode_easy】933. Number of Recent Calls
problem 933. Number of Recent Calls 参考 1. Leetcode_easy_933. Number of Recent Calls; 完
- 【LeetCode】933. Number of Recent Calls 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 队列 相似题目 参考资料 日期 题目地址: ...
- LeetCode - Number of Recent Calls
Write a class RecentCounter to count recent requests. It has only one method: ping(int t), where t r ...
- 109th LeetCode Weekly Contest Number of Recent Calls
Write a class RecentCounter to count recent requests. It has only one method: ping(int t), where t r ...
- C#LeetCode刷题之#933-最近的请求次数(Number of Recent Calls)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4134 访问. 写一个 RecentCounter 类来计算最近的 ...
- LeetCode.933-最近通话次数(Number of Recent Calls)
这是悦乐书的第357次更新,第384篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第219题(顺位题号是933).写一个类RecentCounter来计算最近的请求. 它 ...
- [Swift]LeetCode933. 最近的请求次数 | Number of Recent Calls
Write a class RecentCounter to count recent requests. It has only one method: ping(int t), where t r ...
- C#版 - Leetcode 191. Number of 1 Bits-题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- [leetcode]200. Number of Islands岛屿个数
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
随机推荐
- php接口安全设计浅谈
接口的安全性主要围绕Token.Timestamp和Sign三个机制展开设计,保证接口的数据不会被篡改和重复调用,下面具体来看: (1)Token授权机制:(Token是客户端访问服务端的凭证)--用 ...
- Django 学习 之ORM简介与单表操作
一.ORM简介 1.ORM概念 对象关系映射(Object Relational Mapping,简称ORM). 简单的说,ORM是通过使用描述对象和数据库之间映射的元数据,将程序中的对象自动持久化到 ...
- Centos7 忘记密码解决方法
一.Centos7 忘记密码解决方法 1.进入单用户模型 重启 Linux 系统主机并出现引导界面时,按下键盘上的 e 键进入内核编辑界面 然后按向下键,找到以“Linux16”开头的行,在该行的最后 ...
- SpringBoot 集成FreeMarker
SpringBoot官方不推荐使用jsp,因为jsp不好发挥SpringBoot的特性.官方推荐使用模板引擎代替jsp,现在很多公司都使用FreeMarker来作为SpringBoot的视图. Spr ...
- js加密(十一)yhz566 md5
1. http://www.yhz566.com/ 2. 登录加密 3. navigator = {}; var rng_psize = 256; var b64map = "ABCDEFG ...
- elasticsearch mapping简单介绍
这两天一直在看elasticsearch相关的内容,看到mapping这一块,就折腾了下. 一般情况下,我们不需要对elasticsearch的mapping进行设置,但如果希望对索引使用自定义的管理 ...
- 【快学springboot】使用springboot发送邮件
前言 在实际项目中,经常需要用到邮件通知功能.比如,用户通过邮件注册,通过邮件找回密码等:又比如通过邮件发送系统情况,通过邮件发送报表信息等等,实际应用场景很多.这篇文章,就教大家通过springbo ...
- 小程序PromiseAll定义
var promiseAll = (funcs, callback) =>{ var promises = [] for(var i=0; i<funcs.length; i++){ pr ...
- 「SCOI2010」幸运数字
传送门 Luogu 解题思路 首先构造出所有的幸运数字. 然后考虑一个幸运数字会产生多少贡献. 对于一个数 \(x\),它在区间 \([l,r]\) 内的倍数的个数为 \(\lfloor \frac{ ...
- 5.7 Nginx 其他模块