[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 ...
随机推荐
- SpringMVC 自定义类型转换
类型转换可以将请求参数转换为指定的类型.指定的格式(数据的格式化),然后传给业务方法的参数. Spring MVC内置了常用的类型转换器.如果内置的类型转换器满足不了需求,可以使用自定义的类型转换. ...
- Genymotion设置代理至BurpSuite和Charles
环境 Genymotion VirtualBox BurpSuite Charles 准备 怎么下载安装就不用说了,因为genymotion要依赖VirtualBox,所以要先把VirtualBox装 ...
- C++ 结构体指针理解
上一篇基础链接https://www.cnblogs.com/xuexidememeda/p/12283845.html 主要说一下链表里面双重指针 先说一下结构体 typedef struct LN ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 辅助类:表格单元格使用了 "bg-danger" 类
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Spring mvc mybatis 查询结果缺少字段 解决方法
参考:https://blog.csdn.net/xiaofeifei8421/article/details/43231815
- 利用Python进行多项式拟合
多项式拟合的简单代码: import matplotlib.pyplot as plt import numpy as np x=[,,,,,,,] y=[,,,,,,,] a=np.polyfit( ...
- ActivePerl 安装
下载 https://www.activestate.com/products/activeperl/downloads/ 链接:https://pan.baidu.com/s/1IXPdYFd5bD ...
- 模板语法(DOM与Vue数据绑定)
Vue.js使用了基于HTML的模板语法,允许开发者声明式的将DOM绑定至底层Vue实例的数据. 插值 文本:{{ }}数据绑定最常见的形式就是使用Mustache语法(双大括号)的文本插值,解释为普 ...
- 【STM32H7教程】第53章 STM32H7的LTDC应用之汉字小字库和全字库制作
完整教程下载地址:http://www.armbbs.cn/forum.php?mod=viewthread&tid=86980 第53章 STM32H7的LTDC应用之汉字小字库 ...
- dstat 监控命令详解
一.工具介绍 dstat的man手册对于该工具的解释: dstat - versatile tool for generating system resource statistics 系统资源多用途 ...