这是悦乐书的第357次更新,第384篇原创

01 看题和准备

今天介绍的是LeetCode算法题中Easy级别的第219题(顺位题号是933)。写一个类RecentCounter来计算最近的请求。

它只有一个方法:ping(int t),其中t代表一些时间(以毫秒为单位)。

返回从3000毫秒前到现在为止的ping数。

[t-3000,t]中任何时间ping都将计数,包括当前ping

每次调用ping都使用比之前严格更大的t值。例如:

输入:inputs = [“RecentCounter”,“ping”,“ping”,“ping”,“ping”],
inputs = [[],[1],[100],[3001],[3002]]
输出:[null,1,2,3,3]

注意

  • 每个测试用例最多可以有10000次ping操作。

  • 每个测试用例都会严格增加t的值来调用ping。

  • 每次调用ping,t的取值范围为1 <= t <= 10^9。

02 第一种解法

题目的意思是每次调用RecentCounter类的ping方法时,计算[t-3000,t]范围内的数有多少,而t每次都会增加,也就是说,由多次调用ping方法组成的t数组,是一个递增数组,而我们只需要每次拿到新的t时,计算[t-3000,t]内的t有多少个。

使用List存储每次调用ping方法时传入的t,然后计数[t-3000,t]内的元素个数。

class RecentCounter {

    List<Integer> list;

    public RecentCounter() {
list = new ArrayList<Integer>();
} public int ping(int t) {
list.add(t);
int min = t-3000, count = 0;
for (Integer num : list) {
if (num >= min && num <= t) {
count++;
}
}
return count;
}
} /**
* Your RecentCounter object will be instantiated and called as such:
* RecentCounter obj = new RecentCounter();
* int param_1 = obj.ping(t);
*/

03 第二种解法

同样的思路,但是要比上面的解法更加高效。

因为t是一个递增的数,并且每次计算时,新的t是肯定包含在其中的,所以我们只需要判断[t-3000,t]中的前半部分t-3000即可,从List的第一位元素开始,如果小于t-3000就移除,直到List中的第一位元素符合[t-3000,t]范围,最会返回Listsize即可。

class RecentCounter {

    List<Integer> list;

    public RecentCounter() {
list = new ArrayList<Integer>();
} public int ping(int t) {
list.add(t);
int i = 0, n = list.size();
while (i < n && list.get(i) < t-3000) {
list.remove(list.get(i));
}
return list.size();
}
} /**
* Your RecentCounter object will be instantiated and called as such:
* RecentCounter obj = new RecentCounter();
* int param_1 = obj.ping(t);
*/

04 第三种解法

从第二种解法中,可以看出t数组是存在一种先后顺序,因为我们永远只需要处理前面先进来的数据,而这一特性,可以联想到队列,因为他们都有先进先出的特性。

每次调用ping方法时,将t入队列,然后判断队列顶部的元素是否小于t-3000,如果小于,就将队列顶部的元素移除,直到队列顶部元素大于等于t-3000,最后返回队列的size即可。

class RecentCounter {

    Queue<Integer> queue;

    public RecentCounter() {
queue = new LinkedList<Integer>();
} public int ping(int t) {
if (queue.isEmpty()) {
queue.offer(t);
return 1;
} else {
int min = t-3000;
while (!queue.isEmpty() && queue.peek() < min) {
queue.poll();
}
queue.offer(t);
}
return queue.size();
}
} /**
* Your RecentCounter object will be instantiated and called as such:
* RecentCounter obj = new RecentCounter();
* int param_1 = obj.ping(t);
*/

05 小结

算法专题目前已连续日更超过六个月,算法题文章225+篇,公众号对话框回复【数据结构与算法】、【算法】、【数据结构】中的任一关键词,获取系列文章合集。

以上就是全部内容,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、转发就是对我最大的回报和支持!

LeetCode.933-最近通话次数(Number of Recent Calls)的更多相关文章

  1. [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 ...

  2. 【Leetcode_easy】933. Number of Recent Calls

    problem 933. Number of Recent Calls 参考 1. Leetcode_easy_933. Number of Recent Calls; 完

  3. [LeetCode] 933. Number of Recent Calls 最近的调用次数

    Write a class RecentCounter to count recent requests. It has only one method: ping(int t), where t r ...

  4. 【LeetCode】933. Number of Recent Calls 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 队列 相似题目 参考资料 日期 题目地址: ...

  5. C#LeetCode刷题之#933-最近的请求次数(Number of Recent Calls)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4134 访问. 写一个 RecentCounter 类来计算最近的 ...

  6. LeetCode - Number of Recent Calls

    Write a class RecentCounter to count recent requests. It has only one method: ping(int t), where t r ...

  7. 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 ...

  8. LeetCode:最少移动次数使得数组元素相等||【462】

    LeetCode:最少移动次数使得数组元素相等||[462] 题目描述 给定一个非空整数数组,找到使所有数组元素相等所需的最小移动数,其中每次移动可将选定的一个元素加1或减1. 您可以假设数组的长度最 ...

  9. LeetCode 287. Find the Duplicate Number (python 判断环,时间复杂度O(n))

    LeetCode 287. Find the Duplicate Number 暴力解法 时间 O(nlog(n)),空间O(n),按题目中Note"只用O(1)的空间",照理是过 ...

随机推荐

  1. gson转换问题

    list集合中integer问题 List<Integer> lists= gson.fromJson(params.get("lists"), new TypeTok ...

  2. CentOS 7 安装 metasploit-framework

    1 一键安装metasploit-framework apt-get install curl,wgetcurl https://raw.githubusercontent.com/rapid7/me ...

  3. CH5101 LICS//hdu5904 LICS

    恭喜我已经正式沦为pj组选手QwQ 标题两个题其实不一样的.这是ch   这是hdu 一.CH上的:裸题,求LICS.n<=3000 经典普及组dp题,题解烂大街了.所以对于这题,只讲细节: $ ...

  4. Windows系统下如何卸载干净mysql

    一.在控制面板中卸载mysql软件 二.卸载过后删除C:\Program Files (x86)\MySQL该目录下剩余了所有文件,把mysql文件夹也删了 三.windows+R运行“regedit ...

  5. 【SQL-自动生成编号】按规则自动生成单据编号 以及并发问题_使用触发器、函数 等

    描述:每种单据新建时,自动生成它的单据编号. 规则如:固定码+日期+流水号 (ABC1603180001) 方法一:触发器 触发器的缺点是,执行了sql之后才看到编码. 测试:流水号不能超过最大数,否 ...

  6. C# 扩展方法——mysql-dapper(MySqlMapperExtensions)

    其他扩展方法详见:https://www.cnblogs.com/zhuanjiao/p/12060937.html 反射比较耗费性能,反射得到属性进行缓存 根据反射得到的属性,进行动态拼接sql语句 ...

  7. 字符串分割SplitString

    C/C++中常用的字符串切割函数有strtok.strtok_s与strtok_r. 1.strtok函数 char* strtok(char* str, const char* delim); 分解 ...

  8. @ApiParam和@RequestHeader接收参数的区别

    一.代码 @ApiOperation(value = "查询学生信息") @GetMapping(value = "/select/student") @Api ...

  9. protoc 编译工具

    在进行开发 protoc 之前,你需要首先在你的计算机中安装 protoc 编译工具. 下载编译工具 进入 Protocol Buffers 的源代码中然后选择发布的版本中,找到对应的版本.项目的链接 ...

  10. python3基础: 元组tuple、 列表list、 字典dict、集合set。 迭代器、生成器

    一.元组: tuple Python 的元组与列表类似,不同之处在于元组的元素不能修改. 元组中的元素值是不允许删除的,但我们可以使用del语句来删除整个元组 tup2 = (111, 22, 33, ...